使用SpringMVC标签时踩的一个坑


<mvc:view-controller>标签

如果我们有些请求只是想跳转页面,不需要来后台处理什么逻辑,我们无法在Action中写一个空方法来跳转,直接在中配置一个如下的视图跳转控制器即可(不经过Action,直接跳转页面)

在jsp页面中写入:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>首页</title>
</head>
<body>
    <a href="/hello_world">Hello World</a> <br />
</body>
</html>

在配置文件写入:

<mvc:view-controller path="/" view-name="index2" />

在controller类写入:

 @RequestMapping("/hello_world")
    public String helloWorld(Model model){
        model.addAttribute("msg","hello world");
        return "hello_springmvc";
    }

 配置成功后,发现你访问其他的页面会失败并且idea出现这个警告

 原因:如果没有<mvc:annotation-driven/>,那么所有的@Controller注解可能就没有解析,所有当有请求时候都没有匹配的处理请求类,就都去<mvc:default-servlet-handler/>即default servlet处理了。

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM