tank2140896
tank2140896
2013-07-09 20:25

SpringMVC页面无法跳转

已采纳

出现问题: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]
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • raychou86 raychou86 8年前

    spring配置文件里加上

    点赞 评论 复制链接分享
  • xieyouping 谢小痞 8年前

    [b]①:[/b]
    public String login(******){
    //TODO sth
    return "/ppp/success";
    }
    SpringMvc直接去找路径为/ppp/success的物理视图,应该写成
    return "/ppp/success.jsp";

    [b]②:[/b]
    也可以这样改:
    public ModelAndView login(***){
    //TODO sth
    return new ModelAndView("ppp/success","username" ,username);
    }
    最后真实物理视图:prefix+"ppp/success"+suffix = /ppp/success.jsp

    点赞 评论 复制链接分享

相关推荐