[Spring MVC] - @ModelAttribute使用

在Spring MVC裏,@ModelAttribute一般使用在Controller方法的參數註解中,用於解釋model entity,但同時,也能夠放在方法註解裏。html

若是把@ModelAttribute放在方法的註解上時,表明的是:該Controller的全部方法在調用前,先執行此@ModelAttribute方法java

 

好比咱們有一個Controller:TestControllerweb

@Controller
@RequestMapping(value="test")
public class PassportController {

    @ModelAttribute
    public void preRun() {
        System.out.println("Test Pre-Run");
    }
    
    @RequestMapping(method=RequestMethod.GET)
    public String index() {
        return "login/index";
    }
    
    @RequestMapping(value="login", method=RequestMethod.POST)
    public ModelAndView login(@ModelAttribute @Valid Account account, BindingResult result)
        :
        :
    }
    
    @RequestMapping(value="logout", method=RequestMethod.GET)
    public String logout() {
        :
        :
    }
    
}

在調用全部方法以前,都會先執行preRun()方法。spring

 

咱們能夠把這個@ModelAttribute特性,應用在BaseController當中,全部的Controller繼承BaseController,便可實如今調用Controller時,先執行@ModelAttribute方法。安全

好比權限的驗證(也能夠使用Interceptor)等session

下面是一個設置request和response的方式(這個未測試,不知有沒線和安全問題)app

package com.my.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.ModelAttribute;

public class BaseController {
    
    protected HttpServletRequest request;  
    protected HttpServletResponse response;  
    protected HttpSession session;
      
    @ModelAttribute
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response){  
        this.request = request;
        this.response = response;
        this.session = request.getSession();
    }
    
}

 


 

 

@ModelAttribute也能夠作爲Model輸出到View時使用,好比:jsp

測試例子測試

package com.my.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.my.controller.bean.Account;

@Controller
@RequestMapping(value="attr")
public class TestModelAttributeController {
    
    private static List<Account> accounts = new ArrayList<Account>();
    {
        accounts.add(new Account());
        accounts.add(new Account());
        
        Account ac1 = accounts.get(0);
        Account ac2 = accounts.get(1);
        
        ac1.setUserName("Robin");
        ac1.setPassword("123123");
        
        ac2.setUserName("Lucy");
        ac2.setPassword("123456");
    }

    @RequestMapping(method=RequestMethod.GET)
    public String index() {
        System.out.println("index");
        return "TestModelAttribute/index";
    }
    
    @ModelAttribute("accounts")
    public List<Account> getAccounts() {
        System.out.println("getAccounts");
        return accounts;
    }
    
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib prefix="st" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>TestModelAttribute</title>
</head>
<body>
    <c:forEach items="${accounts}" var="item">
        <c:out value="${item.userName}"></c:out><br/>
    </c:forEach>
</body>
</html>

頁面將輸出:ui

在Console中輸出爲:

 

這裏能夠看到,運行的前後次序爲:先調用getAccounts(),再調用index()。

相關文章
相關標籤/搜索