出现问题: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
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>
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 -->
EncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
EncodingFilter
/*
<!-- 加载所有Spring配置文件 -->
contextConfigLocation
/WEB-INF/config/spring-common.xml
<!-- 配置spring监听 -->
org.springframework.web.context.ContextLoaderListener
<!-- 配置SpringMVC -->
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/config/spring-common.xml
1
springMVC
/
[/code]
2、spring-common.xml
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>
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注解 -->
<!-- 静态资源(js/image)的访问 -->
<!-- 定义视图解析器 -->
[/code]
3、UserLoginAction
[code="java"]
package 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";
}
}
}
[/code]
4、index.jsp
[/code]
用户名
密 码
[/code]