2014-12-06 139 views
0

我已經很難獲得基本的jsp文件在瀏覽器中呈現。我讓它工作正常,但後來我試圖製作一個header.jsp和一個footer.jsp文件以包含在我的student.jsp文件中。當我抽象出來的時候是我開始有問題的時候。Spring:沒有找到具有URI的HTTP請求的映射

我是很新的Spring MVC的,所以耐心是讚賞:)

當我去我得到的錯誤:本地主機:8080/myprojectStudentCourses /學生

從控制檯:

2014年12月6日下午12點52分37秒org.springframework.web.servlet.PageNotFound noHandlerFound警告:未找到HTTP請求與URI [/myprojectStudentCourses/WEB-INF/jsp/student.jsp]在映射 調度程序名爲 'myprojectStudentCourses'

的myproject-servlet.xml中

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

    <context:component-scan base-package="com.myproject" /> 

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

</beans> 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4"> 
    <display-name>myproject Student Courses</display-name> 
    <servlet> 
    <servlet-name>myprojectStudentCourses</servlet-name> 
    <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>myprojectStudentCourses</servlet-name> 
    <url-pattern>/*</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
    <servlet-name>myprojectStudentCourses</servlet-name> 
    <url-pattern>*.jsp</url-pattern> 
    </servlet-mapping> 
</web-app> 

student.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<%@include file="header.jsp" %> 
<form:form method="POST" action="/myprojectStudentCourses/addStudent"> 
    <table> 
    <tr> 
     <td><form:label path="name">Name</form:label></td> 
     <td><form:input path="name" /></td> 
    </tr> 
    <tr> 
     <td><form:label path="age">Age</form:label></td> 
     <td><form:input path="age" /></td> 
    </tr> 
    <tr> 
     <td><form:label path="id">id</form:label></td> 
     <td><form:input path="id" /></td> 
    </tr> 
    <tr> 
     <td colspan="2"> 
      <input type="submit" value="Submit"/> 
     </td> 
    </tr> 
</table> 
</form:form> 
<%@include file="footer.jsp" %> 
ervlet

控制器

package com.myproject; 

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

@Controller 
public class StudentController { 

    @RequestMapping(value = "/student", method = RequestMethod.GET) 
    public ModelAndView student() { 
     return new ModelAndView("student", "command", new Student()); 
    } 

    @RequestMapping(value = "/addStudent", method = RequestMethod.POST) 
    public String addStudent(@ModelAttribute("SpringWeb")Student student, 
    ModelMap model) { 
     model.addAttribute("name", student.getName()); 
     model.addAttribute("age", student.getAge()); 
     model.addAttribute("id", student.getId()); 

     return "result"; 
    } 
} 
+2

刪除與* .JSP您的網址映射條目在web.xml – Angad 2014-12-06 18:01:11

+1

呀,你已經映射/ *,這樣所有的文件名會去調度。所以你不需要* .jsp – zawhtut 2014-12-06 18:02:28

+0

啊!你們真棒...我也必須改變/ *爲只是一個/ – roundtheworld 2014-12-06 18:04:48

回答

1

感謝•安格德和zawhtut,我改變了我的web.xml以下和它的作品!

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4"> 
    <display-name>myproject Student Courses</display-name> 
    <servlet> 
    <servlet-name>myprojectStudentCourses</servlet-name> 
    <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>myprojectStudentCourses</servlet-name> 
    <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 
相關問題