【发布时间】:2015-08-15 14:50:07
【问题描述】:
我在配置 Spring MVC 应用程序时遇到了很大的问题。无论我在配置 xml 中写什么,在到达 localhost/appname/ 或 localhost/appname/register 时都会得到 404。为什么会这样?阅读 Maven 中的 MVC 教程,看起来我正在按照他们说的做所有事情。
web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Wymysl to!</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<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>
</web-app>
调度程序-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
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">
<context:component-scan base-package="wymysl.Controllers" />
<context:annotation-config />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
</beans>
RegisterController.java
package wymysl.Controllers;
import org.springframework.beans.factory.annotation.Autowired;
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 wymysl.database.Person;
import wymysl.database.PersonsRepository;
@Controller
public class RegisterController {
@Autowired
PersonsRepository repo;
@RequestMapping(value = "/register", method = RequestMethod.GET)
public String register() {
return "register";
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String register(@ModelAttribute("Person") Person person) {
System.out.println(""+person.getName());
return "index";
}
}
【问题讨论】:
-
其实你没有映射“/”。尝试拨打
localhost/appname/register -
也试过了,但没有效果
-
@Mike Raphael 是对的,您的控制器中没有这样的“/”映射,@RequestMapping("/") public String showIndexPage() { return "index"; }
-
@Mike 还有一件事,当我在本地(通过 Tomcat 7)运行我的网络应用程序时,我的 URL 还包括我在 Tomcat 中使用的端口号,即:localhost:8080/appname/登记。不确定这是否是绝对必要的,但这就是我在本地找到我的 jsps 的方式可能值得一试
-
@smoggers 好点。
标签: java spring maven spring-mvc web-applications