问题描述
出现问题:SpringMVC页面无法跳转,老是报:[WARN ] [19:58:55] org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/MVCdemo/user/login] in DispatcherServlet with name 'springMVC'之前的项目也是这么写的,毫无问题,现在这个test项目也是简单模仿的,连Hibernate数据库都没用,居然出现这个问题,百度、google搜寻也无果,解决不了。。。下面贴几段核心配置和写法,完整程序见附件,导包的话只要导spring最新版本即可。1、web.xml<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!-- Character Encoding filter --> <filter> <filter-name>EncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 加载所有Spring配置文件 --><context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/config/spring-common.xml </param-value></context-param><!-- 配置spring监听 --><listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 配置SpringMVC --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/spring-common.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>2、spring-common.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 声明使用注解的风格 --><context:annotation-config/><!-- 开启mvc注解 --><mvc:annotation-driven /> <!-- 静态资源(js/image)的访问 --> <mvc:resources location="/js/" mapping="/js/**"/> <!-- 定义视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>3、UserLoginActionpackage com.study.action;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import com.study.service.UserService;@Controller@RequestMapping("/user")public class UserLoginAction{@Resource(name="userService")private UserService userService;@RequestMapping("/login")public String login(String username,String password,HttpServletRequest request){boolean b = userService.search(username,password);if(b==true){request.setAttribute("username",username);return "/ppp/success";}else{return "/ppp/fail"; }}}4、index.jsp <form action="/MVCdemo/user/login" method="post"> 用户名<input type="text" name="username"><br /> 密 码<input type="password" name="password"><br /> <input type="submit" value="提交"> </form>
解决方案
spring配置文件里加上<context:component-scan base-package="*" />
解决方案二:
①:public String login(******){ //TODO sth return "/ppp/success";}SpringMvc直接去找路径为/ppp/success的物理视图,应该写成return "/ppp/success.jsp";②:也可以这样改:public ModelAndView login(***){ //TODO sth return new ModelAndView("ppp/success","username" ,username);}最后真实物理视图:prefix+"ppp/success"+suffix = /ppp/success.jsp