【发布时间】:2014-12-06 17:56:25
【问题描述】:
我很难获得一个基本的 jsp 文件以在我的浏览器中呈现。我让它工作正常,但后来我尝试制作一个 header.jsp 和一个 footer.jsp 文件以包含在我的 student.jsp 文件中。当我把它抽象出来时,我就开始遇到问题了。
我是 Spring MVC 的新手,非常感谢您耐心等待 :)
我去的时候报错:localhost:8080/myprojectStudentCourses/student
从控制台是:
2014 年 12 月 6 日下午 12:52:37 org.springframework.web.servlet.PageNotFound noHandlerFound 警告:未找到带有 URI 的 HTTP 请求的映射 [/myprojectStudentCourses/WEB-INF/jsp/student.jsp] 在 名称为“myprojectStudentCourses”的 DispatcherServlet
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" %>
控制器
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";
}
}
【问题讨论】:
-
在 web.xml 中删除带有 *.jsp 的 url 映射条目
-
是的,你已经映射了 /* 所以所有的文件名都将发送给调度程序。所以你不需要 *.jsp
-
啊!你们太棒了...我还不得不将 /* 更改为 /
标签: java spring jsp spring-mvc servlets