java 未找到 Spring MVC 404 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34735399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC 404 not found error
提问by Harshit Shrivastava
I am trying to run a project in Spring MVC. Here is the code
我正在尝试在 Spring MVC 中运行一个项目。这是代码
index.jsp
索引.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome to Spring Web MVC project</title>
</head>
<body>
<h1>Spring 3 Register!</h1>
<a href="register.htm">click</a>
<form:form action="${pageContext.request.contextPath}/register.htm" method="POST" modelAttribute="userForm">
<table>
<tr>
<td colspan="2" align="center">Spring MVC Form Demo - Registration</td>
</tr>
<tr>
<td>User Name</td>
<td><form:input path="username" /></td>
</tr>
<tr>
<td>Password</td>
<td><form:password path="password" /></td>
</tr>
<tr>
<td>Email</td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td>BirthDate (mm/dd/yyyy)</td>
<td><form:input path="birthDate" /></td>
</tr>
<tr>
<td>Profession</td>
<td><form:select path="profession" items="${professionList}" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Register" /></td>
</tr>
</table>
</form:form>
</body>
</html>
RegistrationController.java
注册控制器.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package RegisterInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
*
* @author Harshit Shrivastava
*/
import RegisterInfo.model.User;
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.ui.Model;
@Controller
@RequestMapping(value = "/register")
public class RegistrationController {
@RequestMapping(method = RequestMethod.GET)
public String viewRegistration(Model model)
{
User userForm = new User();
model.addAttribute("userForm", new User());
/*List<String> professionList = new ArrayList();
professionList.add("Developer");
professionList.add("Designer");
professionList.add("IT Manager");
model.put("professionList", professionList);*/
return "index";
}
@RequestMapping(method = RequestMethod.POST)
public String processRegistration(@ModelAttribute("userForm") User user, Map<String, Object> model)
{
System.out.println("Username : " + user.getUserName());
model.put("userForm", new User());
return "index";
}
}
User.java
用户.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package RegisterInfo.model;
/**
*
* @author Harshit Shrivastava
*/
import java.util.Date;
public class User {
private String username;
private String password;
private String email;
private Date birthDate;
private String profession;
public String getUserName()
{
return username;
}
public void setUserName(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public Date getBirthDate()
{
return birthDate;
}
public void setBirthDate(Date birthDate)
{
this.birthDate = birthDate;
}
public String getProfession()
{
return profession;
}
public void setProfession(String profession)
{
this.profession = profession;
}
}
web.xml
网页.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<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>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
</web-app>
dispatcher-servlet.xml
调度程序-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd ">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<context:component-scan base-package="SpringRegister" />
<mvc:annotation-driven />
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
Project Structure :
项目结构:
In the above program, I am always getting the 404 not found status
. I tried
在上面的程序中,我总是得到404 not found status
. 我试过
http://localhost:8080/RegisterInfoMaven/index
http://localhost:8080/RegisterInfoMaven/index
http://localhost:8080/RegisterInfoMaven/index.htm
http://localhost:8080/RegisterInfoMaven/index.htm
both not working.
两者都不工作。
When I remove web.xml & dispatcher-servlet.xml from the project, then http://localhost:8080/RegisterInfoMaven/index.jsp
loads fine, but as I put web.xml & dispatcher-servlet.xml
, then again it starts giving 404 not found
error.
当我从项目中删除 web.xml 和 dispatcher-servlet.xml 时,然后http://localhost:8080/RegisterInfoMaven/index.jsp
加载正常,但正如我所说web.xml & dispatcher-servlet.xml
,然后它又开始给出404 not found
错误。
What is wrong with web.xml & dispatcher-servlet.xml
files ?
web.xml & dispatcher-servlet.xml
文件有什么问题?
回答by Ken Bekov
First. The mapping in your web.xml
file:
第一的。您web.xml
文件中的映射:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
means, that dispatcher servlet will process only requests ending with .html
. Change it to next one:
意味着,调度程序 servlet 将只处理以.html
. 改成下一个:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
After that dispatcher servlet will process all of incoming requests.
之后,调度程序 servlet 将处理所有传入的请求。
Second. Resolver's declaration from your dispatcher-servlet.xml
:
第二。来自您的解析器声明dispatcher-servlet.xml
:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
means, that when your Controller's method returns String
value, resolver will add /WEB-INF/jsp
before and .jsp
after any value. And then will be looking for file with this name. So, you need to create jsp
directory in you WEB-INF
directory and put jsp files there.
意味着,当您的 Controller 的方法返回String
值时,解析器将在任何值/WEB-INF/jsp
之前和.jsp
之后添加。然后将查找具有此名称的文件。所以,你需要jsp
在你的WEB-INF
目录中创建目录并将jsp文件放在那里。
Third. When you type something like http://localhost:8080/index, spring will try to find Controller class or methodmarked with @RequestMapping(value = "/index")
, but not fileindex.jsp.
第三。当您键入类似http://localhost:8080/index 的内容时,spring 将尝试查找标有 的Controller类或方法@RequestMapping(value = "/index")
,而不是文件index.jsp。
Hence, you have to declare Controller's method, marked with @RequestMapping(value = "/index")
. And your request string will be looks somethin like http://localhost:8080/indexwithout any extensions.
因此,您必须声明 Controller 的方法,标记为@RequestMapping(value = "/index")
. 您的请求字符串将类似于http://localhost:8080/index ,没有任何扩展名。
回答by Abdelhak
Try to write your url in web.xml
like this:
尝试这样写你的网址web.xml
:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*.htm</url-pattern>
And:
和:
<welcome-file-list>
<welcome-file>/index.htm</welcome-file>
</welcome-file-list>
And try to replace this:
并尝试替换它:
<form:form action="${pageContext.request.contextPath}/register.htm" method="POST" modelAttribute="userForm"
With this posting the right path ${pageContext.request.contextPath}/register
:
有了这个发布正确的路径${pageContext.request.contextPath}/register
:
<form:form action="${pageContext.request.contextPath}/register" method="POST" modelAttribute="userForm">
And adding the value User userForm
instead User user
like this:
并增加价值User userForm
,而不是User user
像这样:
public String processRegistration(@ModelAttribute("userForm") User userForm, Map<String, Object> model)
And change the base-package
like:
并更改base-package
如下:
<context:component-scan base-package="RegisterInfo.controller" />