2015-08-21 158 views
1

我看起來像一個菜鳥錯誤,但似乎我無法通過它。我正在學習JSP和Spring,並且在我的項目中遇到了404錯誤。 我正在使用一個tomcat 8.0本地服務器。我的目標是從「http://localhost:8080/TestSpring/vues/bonjour.jsp」去「http://localhost:8080/TestSpring/bonjour」有教程的幫助,但是這會導致一個404簡單的JSP頁面404錯誤

「調度員servlet.xml中」

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 

<context:component-scan base-package="be.knoware" /> 

<bean 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix"> 
     <value>/vues/</value> 
    </property> 
    <property name="suffix"> 
     <value>.jsp</value> 
    </property> 
</bean> 

<bean id="messageSource" 
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <property name="basename" value="classpath:messages" /> 
    <property name="defaultEncoding" value="ISO-8859-1" /> 
</bean> 

網絡。 XML

<!DOCTYPE web-app PUBLIC 
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd" > 

<web-app> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> 
    </context-param> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Declaration de la servlet de Spring et de son mapping --> 
    <servlet> 
     <servlet-name>servlet-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>servlet-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

bonjour.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" isELIgnored="false" 
    pageEncoding="ISO-8859-1"%> 
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 
     <title><spring:message code="titre.bonjour"/> ${personne}</title> 
    </head> 
    <body> 
     <spring:message code="libelle.bonjour.lemonde" arguments="${personne}"/> 
    </body> 
</html> 

BonjourController.java

package controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

@Controller 
@RequestMapping("/bonjour") 

public class BonjourController { 

    @RequestMapping(method = RequestMethod.GET) 
    public String afficherBonjour(final ModelMap pModel){ 
     pModel.addAttribute("personne","Jim"); 
     return "bonjour"; 
    } 
} 

如果它可能會幫助,該項目。

enter image description here

感謝提前:)

+0

是您的應用程序成功地部署在服務器? –

+0

@Subodh是的,我想是的(如果這是你的意思,部署[鏈接](http://imgur.com/AOrBi8l)),並有我的服務器啓動時的控制檯文本,我嘗試連接到URL 。 [鏈接](http://pastebin.com/zvXLQfn7) – Erunayc

+0

在日誌中會清楚地顯示項目是否已部署 –

回答

1

你可以做一些措施,如果有一些部署問題或服務器問題

  • 清理並生成項目,並複製戰爭文件中您的服務器
  • 在更新war文件之前,您必須關閉tomcat或Server。
  • 啓動服務器和打開URL和檢查戰爭提取自動或不,如果是的話沒有問題的服務器。
  • 檢查URL和日誌文件以獲得更好的保證。 樂意幫助
+0

非常感謝你,我發現如何檢查部署,它實際上已部署:) – Erunayc

1

以及我在評論中發現,他們如果您的應用程序正在正常與否部署,因爲你的web.xml看起來不那麼有效的點quessing。

i) 只有當您想要將全局應用程序上下文傳遞給所有servlet時纔會使用此類配置,就像您要加載spring-context兩次一樣。 你可以找到在here

<context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> 
    </context-param> 

所以,如果你不想通過一個全球性的appContext,然後從你的web.xml文件中刪除以下行的詳細信息

<context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

II)

在servlet映射將URL格式從/更改爲/ * 所以它會是這樣的:

<servlet-mapping> 
     <servlet-name>servlet-dispatcher</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 

iii) 在您的調度程序servlet中。XML添加軟件包,其中控制器是 原因它不是由彈簧

<context:component-scan base-package="controller" /> 
1

被處理以基於XML的MultiActionController,必須配置該方法名稱解析器(InternalPathMethodNameResolver,PropertiesMethodNameResolver或使用ParameterMethodNameResolver)來映射的URL到特定的方法名稱。

首先,您可以嘗試將Controller Controller添加到dispatcher-servlet.xml中;

<!-- Register the bean --> 
<bean class="yourPackage.controller.BonjourController " /> 

如果它不工作嘗試ViewResolvers。

0

所以,最後這是我的一個誤解。事實上,我在我的dispatcher-servlet.xml中使用了錯誤的base-package ......至少這篇文章教會了我在J2E中的一些良好習慣以及如何使用cfg tomcat!

無論如何,THX您的時間:)