我尝试春天3 mvc这个包 org.spring.test和代码是
@Controller
@RequestMapping("/welcome")
public class WelcomeController {
private Logger logger = org.slf4j.LoggerFactory.getLogger(WelcomeController.class);
@RequestMapping(method = RequestMethod.GET)
public void welcome() {
logger.info("Welcome!");
}
@RequestMapping("test1")
public void test1() {
logger.info("test1!");
}
@RequestMapping("test2")
public void test2() {
logger.info("test2!");
}
}
web.xml是:
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
最后一个mvc.xml是
<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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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="org.spring.test" />
<!-- Configures support for @Controllers -->
<mvc:annotation-driven />
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
当我使用“mvn jetty:run”命令并写入http://localhost:8080/springdemo/welcome时 是
"HTTP ERROR 404
Problem accessing /springdemo/WEB-INF/views/welcome.jsp. Reason:
NOT_FOUND"
控制台消息是:
INFO : org.spring.test.WelcomeController - Welcome!
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/springdemo/WEB-INF/views/welcome.jsp] in DispatcherServlet with name 'Spring MVC Dispatcher Servlet'
为什么?
答案 0 :(得分:2)
看起来你缺少/WEB-INF/views/test.jsp。这是真的吗?
您正在请求/测试URI,它未映射到您的控制器类(我认为映射到/ welcome)。 Spring使用InternalResourceViewResolver来处理您的请求。它需要前缀+“test”+后缀。这就是你最终得到/WEB-INF/views/test.jsp。
确保您拥有该JSP或更改控制器映射。
更新: 好的,你改变了问题。但是你有/springdemo/WEB-INF/views/welcome.jsp吗?