我遇到了麻烦,希望得到你的帮助。我是Spring MVC的初学者(和Spring一样)。我已经关注http://www.mkyong.com/spring3/spring-3-mvc-hello-world-example/,但它没有继续工作。我添加了一个欢迎文件(index.jsp)。当我输入(http:// localhost:8080 / SpringMVC)时。但是当我添加控制器模式(http:// localhost:8080 / SpringMVC / welcome)时,它不起作用(HTTP状态404)。在这里我的配置:
的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>Spring Web MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
MVC-调度-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.mkyong.common.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
我的文件夹结构是:
-> src
-> main
-> java
-> com
-> mkyong
-> common
-> controller
-> HelloController.java
-> resources
-> webapp
-> index.jsp
-> WEB-INF
-> mvc-dispatcher-servlet.xml
-> web.xml
-> pages
-> hello.jsp
有人可以帮助我吗?
答案 0 :(得分:1)
404表示找不到请求的资源。确保您的控制器注释为:
@Controller and @RequestMapping("/welcome")
从链接:
@Controller
@RequestMapping("/welcome")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Spring 3 MVC Hello World");
return "hello";
}
}
答案 1 :(得分:0)
您的servlet映射到'/',您想要命中的控制器接受'/ welcome'的请求
您需要提出的请求应该是( http:// localhost:8080 / welcome )。我不知道他为什么在他的例子中有额外的'SpringMVC'。
此外,将<mvc:annotation-driven/>
添加到mvc-dispatcher-servlet.xml
以确保它在您的控制器上使用注释。并添加mvc XML命名空间。我的命名空间如下所示:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
我建议删除该欢迎文件,直到你的控制器正常工作以保持简单
答案 2 :(得分:0)
您必须将控制器映射到“/".
@Controller
@RequestMapping("/")
public class StartController {
@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Spring 3 MVC Hello World");
return "hello";
}
}
答案 3 :(得分:0)
在您的视图中添加此库以启用${}
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
这将解决您的问题。