使用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