使用SpringMVC<mvc:view-controller/>标签时踩的一个坑

<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处理了。

 

猜你喜欢

转载自www.cnblogs.com/zy527/p/11705091.html
今日推荐