struts2
概念:Struts2是一個基于MVC設計模式的Web應用框架猫缭,它本質(zhì)上相當于一個servlet误续,在MVC設計模式中闪金,Struts2作為控制器(Controller)來建立模型與視圖的數(shù)據(jù)交互羡滑。
開發(fā)環(huán)境搭建
1潘拨、拷jar包绩衷, 在apps包下蹦魔,blank>WEB-INF>lib
2、拷配置文件咳燕, 在apps包下勿决,blank>WEB-INF>src>java>struts.xml
3、拷過濾器配置招盲, 在apps包下低缩, blank>WEB-INF>web.xml
4、將自己新建的action類配置到struts.xml中
public String execute(){
return "tohello";
}
<package name="default" namespace="/" extends="struts-default">
<!-- package的namespace與action的name組成 瀏覽器訪問路徑 class是自己新建action的全類名 -->
<action name="hello" class="com.hemi.action.ActionTest">
<!-- name自己新建action類return的字符串 /hello.jsp跳轉(zhuǎn)的目標jsp -->
<result name="tohello">/hello.jsp</result>
</action>
</package>
struts2 核心類:StrutsPrepareAndExecuteFilter
struts2 執(zhí)行順序
配置文件
-
constant 標簽:
- name="struts.devMode" value="true" 設置配置文件更改不會自動重新加載
- name="struts.i18n.encoding" value="UTF-8" 設置框架的編碼格式,默認UTF-8
- name="struts.action.extension" value="do,," 設置頁面訪問時后綴名 默認action,, ,,表示加或者不加后綴都能訪問
-
package 標簽:
- name:用來區(qū)分不同的action
- extends:必須默認繼承 struts-default
-
action 標簽: 配置action處理類路徑 和映射路徑
- method:指明要調(diào)用action中的哪個方法咆繁,不寫默認執(zhí)行execute方法
-
result 標簽: 配置結(jié)果頁面
- name:邏輯視圖名讳推,是action類返回的邏輯視圖名
- 文本內(nèi)容:物理視圖名(jsp文件)
include 標簽:用來引入其他配置文件,適合團隊分模塊開發(fā)
action編寫方式
1玩般、自定義類银觅,編寫execute方法
2、實現(xiàn)Action接口
3坏为、繼承ActionSupport類
action方法訪問方式
1究驴、傳統(tǒng)訪問,通過method屬性指明
2匀伏、通配符訪問 *
<!-- {表示第幾個*號是方法名} 根據(jù)*輸入的方法名 并根據(jù)該方法返回的字符串與resultname比對進行相應的跳轉(zhuǎn)-->
<action name="user_*" class="........." method="{1}">
<result name="ok"></result>
<result name="fail"></result>
</action>
3洒忧、動態(tài)方法調(diào)用 ! action名够颠!方法名.后綴 放開動態(tài)調(diào)用的常量是:struts.enable.DynamicMethodInvocation 為true 開啟
跳轉(zhuǎn)方式
- result 標簽:type 屬性來控制頁面的跳轉(zhuǎn)方式
- jsp頁面跳轉(zhuǎn)(默認)轉(zhuǎn)發(fā) dispatcher 重定向 redirect
- Action之間跳轉(zhuǎn) : 轉(zhuǎn)發(fā) chain 重定向redirectAction
<!-- 默認轉(zhuǎn)發(fā) dispatcher -->
<result name="login">/login.jsp</result>
<result name="register">/register.jsp</result>
<!-- jsp頁面的重定向 -->
<result name="menu" type="redirect">/menu.jsp</result>
<!-- action之間的重定向 -->
<result name="fail" type="redirectAction">user_register</result>
注意不同papackage標簽的Action之間的跳轉(zhuǎn) 需要注入
通過兩個param標簽(放在result標簽內(nèi)) 注入 name="actionName" 和 name="nameSpace"
<result name="success" type="redirectAction">
<!-- 另一個package中action的name -->
<param name="actionName">hello</param>
<!-- 另一個package的namespace -->
<param name="nameSpace">/</param>
</result>
動態(tài)結(jié)果頁面配置
1熙侍、在action類中定義一個字符串變量,并提供get方法 在每個方法內(nèi)設置該字符串來決定物理視圖名履磨,指定要跳轉(zhuǎn)的目標頁面
public class MyAction {
private String view;
public String getView() {
return view;
}
public void setView(String view) {
this.view = view;
}
public String test1(){
setView("test1.jsp");
return "ok";
}
public String test2(){
setView("test2.jsp");
return "ok";
}
public String test3(){
setView("test3.jsp");
return "ok";
}
}
2蛉抓、在result 的文本配置成獲取物理視圖屬性 通過${action中的字符串變量名}
<action name="test_*" class="com.hemi.action.MyAction" method="{1}">
<result name="ok">/${view}</result>
</action>
Servlet API的操作,也就是獲取參數(shù)
1蹬耘、耦合方式 ServletActionContext.getRequest()
//耦合方式
HttpServletRequest req = ServletActionContext.getRequest();
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println(username);
System.out.println(password);
if ("admin".equals(username) && "123".equals(password)) {
req.setAttribute("username", username);
req.setAttribute("password", password);
setView("success.jsp");
return "ok";
}
setView("login.jsp");
return "fail";
2芝雪、解耦方式 ActionContext.getContext()
//解耦方式
ActionContext context = ActionContext.getContext();
Map<String, Object> map = context.getParameters();
String[] username = (String[]) map.get("username");
setView("test3.jsp");
return "ok";
3、ioc注入 實現(xiàn)ServletRequestAware接口在類里聲明HttpServletRequest
并重寫setServletRequest方法 在里面寫this.request=req;(給類中的全局變量賦值)
public class MyAction implements ServletRequestAware {
private String view;
private HttpServletRequest request;
//ioc注入
public String test2() {
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username);
System.out.println(password);
if ("admin".equals(username) && "123".equals(password)) {
request.setAttribute("username", username);
request.setAttribute("password", password);
setView("success.jsp");
return "ok";
}
setView("login.jsp");
return "fail";
}
@Override
public void setServletRequest(HttpServletRequest req) {
this.request=req;
}
}
數(shù)據(jù)校驗
1综苔、首先繼承ActionSupport
2惩系、校驗全部方法 重寫 validate方法
3、校驗指定方法 自定義方法 validate+校驗的方法名(注意:方法首字母大寫)
4如筛、添加校驗錯誤信息 addFieldError
//System.out.println("校驗test1方法");
String username = request.getParameter("username");
if (username.length()==0) {
addFieldError("username", "用戶名不能為空");
}
5堡牡、配置文件中加一個result name為input
<result name="input" type="redirect">/login.jsp</result>