BaseController作父類
- (1)子類繼承父類的好處
- (2)springmvc可以給控制器方法引數賦值
request,response,session - (3)springmvc可以給控制器成員變數賦值
//子類繼承父類,可以自動擁有父類的非私有成員(方法或者變數)
public class BaseController {
//定義一個可以回傳companyId
public String getLoginCompanyId(){
return "1";
}
//定義一個可以回傳companyName
public String getLoginCompanyName(){
return "吉首大學";
}
}
引數為request
@RequestMapping(path="/testRequest",method ={ RequestMethod.GET})
public String testRequest(HttpServletRequest request){//springmvc會賦值
l.info("testRequest request="+request.getParameter("age"));
request.setAttribute("jack","rose");
return "result";
}
<body>
我是結果頁面
${jack}
</body>
成員變數為request等
@Autowired
HttpSession httpSession;
最終版本
//1》子類繼承父類,可以自動擁有父類的非私有成員(方法或者變數)
public class BaseController {
//定義一個可以回傳companyId
public String getLoginCompanyId(){
return "1";
}
//定義一個可以回傳companyName
public String getLoginCompanyName(){
return "吉首大學";
}
//2 如果在父類中定義成員變數 request,session,response,并且注入物件
// 以后控制器方法可以直接使用
@Autowired
protected HttpServletRequest request;
//注入session
@Autowired
protected HttpSession session;
//注入response
@Autowired
protected HttpServletResponse response;//需要disable inspection
}
- 繼承 BaseController的類可以呼叫獲取companyId與companyName的方法
- 控制器方法還可以直接使用request,response,session等物件
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/198602.html
標籤:其他
