- 緩存問題:清空服務(wù)器項(xiàng)目(remove),選中項(xiàng)目-project-撤掉勾選buildauto-clean-選中項(xiàng)目右鍵buildproject-勾選自動(dòng)創(chuàng)建- run
- ctrl+alt+t關(guān)聯(lián)源文件,導(dǎo)包就導(dǎo)zip就行
-
window-showview-other-Javadoc可以看自帶文檔
image.png
1.結(jié)果跳轉(zhuǎn)方式
-----------分四種
<!-- 轉(zhuǎn)發(fā) -->
<action name="Demo1Action" class="cn.itheima.a_result.Demo1Action" method="execute" >
<result name="success" type="dispatcher" >/hello.jsp</result>
</action>
<!-- 重定向 -->
<action name="Demo2Action" class="cn.itheima.a_result.Demo2Action" method="execute" >
<result name="success" type="redirect" >/hello.jsp</result>
</action>
<!-- 轉(zhuǎn)發(fā)到Action -->
<action name="Demo3Action" class="cn.itheima.a_result.Demo3Action" method="execute" >
<result name="success" type="chain">
<!-- action的名字 -->
<param name="actionName">Demo1Action</param>
<!-- action所在的命名空間 -->
<param name="namespace">/</param>
</result>
</action>
<!-- 重定向到Action -->
<action name="Demo4Action" class="cn.itheima.a_result.Demo4Action" method="execute" >
<result name="success" type="redirectAction">
<!-- action的名字 -->
<param name="actionName">Demo1Action</param>
<!-- action所在的命名空間 -->
<param name="namespace">/</param>
</result>
</action>
2.訪問api方式
-
action是每次訪問都會(huì)創(chuàng)建,servlet是有線程安全問題,多個(gè)請(qǐng)求會(huì)訪問一個(gè)servlet
image.png 訪問方式
- 最常用
public String execute() throws Exception {
//request域=> map (struts2并不推薦使用原生request域)
//不推薦
Map<String, Object> requestScope = (Map<String, Object>) ActionContext.getContext().get("request");
//推薦
ActionContext.getContext().put("name", "requestTom");
//session域 => map
Map<String, Object> sessionScope = ActionContext.getContext().getSession();
sessionScope.put("name", "sessionTom");
//application域=>map
Map<String, Object> applicationScope = ActionContext.getContext().getApplication();
applicationScope.put("name", "applicationTom");
return SUCCESS;
}
------ServletActionContext工具類來獲取
//并不推薦
public String execute() throws Exception {
//原生request
HttpServletRequest request = ServletActionContext.getRequest();
//原生session
HttpSession session = request.getSession();
//原生response
HttpServletResponse response = ServletActionContext.getResponse();
//原生servletContext
ServletContext servletContext = ServletActionContext.getServletContext();
return SUCCESS;
}
-------------了解通過實(shí)現(xiàn)接口方式
//如何在action中獲得原生ServletAPI
public class Demo7Action extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request;
public String execute() throws Exception {
System.out.println("原生request:"+request);
return SUCCESS;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}
3.獲得參數(shù)
-
strutsMVC
image.png - Action生命周期
1.每次請(qǐng)求到來時(shí),都會(huì)創(chuàng)建一個(gè)新的Action實(shí)例
2.Action是線程安全的.可以使用成員變量接收參數(shù)
------------- 屬性參數(shù)
<form action="${pageContext.request.contextPath}/Demo8Action">
用戶名:<input type="text" name="name" /><br>
年齡:<input type="text" name="age" /><br>
生日:<input type="text" name="birthday" /><br>
<input type="submit" value="提交" />
</form>
//準(zhǔn)備與參數(shù)鍵名稱相同的屬性
private String name;
//自動(dòng)類型轉(zhuǎn)換 只能轉(zhuǎn)換8大基本數(shù)據(jù)類型以及對(duì)應(yīng)包裝類
private Integer age;
//支持特定類型字符串轉(zhuǎn)換為Date ,例如 yyyy-MM-dd
private Date birthday;
--------------------對(duì)象參數(shù)
<form action="${pageContext.request.contextPath}/Demo9Action">
用戶名:<input type="text" name="user.name" /><br>
年齡:<input type="text" name="user.age" /><br>
生日:<input type="text" name="user.birthday" /><br>
<input type="submit" value="提交" />
</form>
//準(zhǔn)備user對(duì)象屬性
private User user;
----------------------模型驅(qū)動(dòng)
<form action="${pageContext.request.contextPath}/Demo8Action">
用戶名:<input type="text" name="name" /><br>
年齡:<input type="text" name="age" /><br>
生日:<input type="text" name="birthday" /><br>
<input type="submit" value="提交" />
</form>
//struts2如何獲得參數(shù)-方式2
public class Demo10Action extends ActionSupport implements ModelDriven<User> {
//準(zhǔn)備user 成員變量
private User user =new User();
public String execute() throws Exception {
System.out.println(user);
return SUCCESS;
}
@Override
public User getModel() {
return user;
}
4.集合類型封裝
-------------list,map屬性
//list
private List<String> list;
//Map
private Map<String,String> map;
<form action="${pageContext.request.contextPath}/Demo11Action" method="post" >
list:<input type="text" name="list" /><br>
list:<input type="text" name="list[3]" /><br>
map:<input type="text" name="map['haha']" /><br>
<input type="submit" value="提交" />
</form>
5.添加客戶
- 注意:struts和hibernate包在合并時(shí).javassist-3.18.1-GA.jar包是重復(fù)的,刪除版本低的.
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
private CustomerService cs = new CustomerServiceImpl();
//添加客戶
public String add() throws Exception {
//1 調(diào)用Service
cs.save(customer);
//2 重定向到列表action方法
return "toList";
}
@Override
public Customer getModel() {
return customer;
}
}
6.ognl表達(dá)式
- OGNL:對(duì)象視圖導(dǎo)航語言. ${user.addr.name} 這種寫法就叫對(duì)象視圖導(dǎo)航.
OGNL不僅僅可以視圖導(dǎo)航.支持比EL表達(dá)式更加豐富的功能. - struts2 的包中已經(jīng)包含了.所以不需要導(dǎo)入額外的jar包
-
內(nèi)部結(jié)構(gòu)
image.png
//準(zhǔn)備工作
public void fun1() throws Exception{
//準(zhǔn)備ONGLContext
//準(zhǔn)備Root
User rootUser = new User("tom",18);
//準(zhǔn)備Context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User("jack",18));
context.put("user2", new User("rose",22));
OgnlContext oc = new OgnlContext();
//將rootUser作為root部分
oc.setRoot(rootUser);
//將context這個(gè)Map作為Context部分
oc.setValues(context);
//書寫OGNL
Ognl.getValue("", oc, oc.getRoot());
}
-------------------------取出root直接取出
//取出root中user對(duì)象的name屬性
String name = (String) Ognl.getValue("name", oc, oc.getRoot());
Integer age = (Integer) Ognl.getValue("age", oc, oc.getRoot());
-------------------------取出context加#取出
//取出context中鍵為user1對(duì)象的name屬性
String name = (String) Ognl.getValue("#user1.name", oc, oc.getRoot());
String name2 = (String) Ognl.getValue("#user2.name", oc, oc.getRoot());
Integer age = (Integer) Ognl.getValue("#user2.age", oc, oc.getRoot());
-------------------------賦值直接在取值語法上書寫,有多個(gè)返回值返回最后一個(gè),賦值可以多個(gè)一起寫逗號(hào)隔開
//將root中的user對(duì)象的name屬性賦值
Ognl.getValue("name='jerry'", oc, oc.getRoot());
String name = (String) Ognl.getValue("name", oc, oc.getRoot());
String name2 = (String) Ognl.getValue("#user1.name='張三',#user1.name", oc, oc.getRoot());
------------------//調(diào)用root中user對(duì)象的setName方法
Ognl.getValue("setName('lilei')", oc, oc.getRoot());
String name = (String) Ognl.getValue("getName()", oc, oc.getRoot());
String name2 = (String) Ognl.getValue("#user1.setName('lucy'),#user1.getName()", oc, oc.getRoot());
-----------------調(diào)用靜態(tài)方法
String name = (String) Ognl.getValue("@cn.itheima.a_ognl.HahaUtils@echo('hello 強(qiáng)勇!')", oc, oc.getRoot());
//Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot());
Double pi = (Double) Ognl.getValue("@@PI", oc, oc.getRoot());
-----------------ognl創(chuàng)建對(duì)象-list|map
//創(chuàng)建list對(duì)象
Integer size = (Integer) Ognl.getValue("{'tom','jerry','jack','rose'}.size()", oc, oc.getRoot());
String name = (String) Ognl.getValue("{'tom','jerry','jack','rose'}[0]", oc, oc.getRoot());
String name2 = (String) Ognl.getValue("{'tom','jerry','jack','rose'}.get(1)", oc, oc.getRoot());
//創(chuàng)建Map對(duì)象
Integer size2 = (Integer) Ognl.getValue("#{'name':'tom','age':18}.size()", oc, oc.getRoot());
String name3 = (String) Ognl.getValue("#{'name':'tom','age':18}['name']", oc, oc.getRoot());
Integer age = (Integer) Ognl.getValue("#{'name':'tom','age':18}.get('age')", oc, oc.getRoot());
7.ognl與Struts2結(jié)合
- 結(jié)合原理:原來的ognlcontext在struts2中改了一個(gè)名字為valuestack值棧。其中root默認(rèn)是當(dāng)前訪問action,context被包裝成了actioncontecxt數(shù)據(jù)中心,并且root和context是相互引用關(guān)系
-
棧原理是先進(jìn)后出,棧是arraylist創(chuàng)建的
image.png
image.png - <s:debug></s:debug>
-
Context部分就是ActionContext數(shù)據(jù)中心
image.png -
struts2與ognl結(jié)合體現(xiàn)
image.png
image.png
image.png - 想要賦值就需要在action之前將user壓入棧中,一張方法是實(shí)現(xiàn)接口(20個(gè)過濾器之一的) <interceptor-ref name="prepare"/>不建議,另外一個(gè)就是實(shí)現(xiàn)modeldriver接口(這個(gè)接口也在action調(diào)用之前)
- 配置文件中重定向傳參${name}(ognl專用在配置文件中的)
<action name="Demo3Action" class="cn.itheima.d_config.Demo3Action" method="execute" >
<result name="success" type="redirectAction" >
<param name="actionName">Demo1Action</param>
<param name="namespace">/</param>
<!-- 如果添加的參數(shù)struts"看不懂".就會(huì)作為參數(shù)附加重定向的路徑之后.
如果參數(shù)是動(dòng)態(tài)的.可以使用${}包裹ognl表達(dá)式.動(dòng)態(tài)取值
-->
<param name="name">${name}</param>
</result>
</action>
-
擴(kuò)展:request對(duì)象的getAttribute方法
image.png - jsp和struts,ognl
-----------------------有var的會(huì)將循環(huán)后取到的對(duì)象放在context中
- #list(ognl語法)
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:iterator value="#list" var="cust" >
<TR
style="FONT-WEIGHT: normal; FONT-STYLE: normal; BACKGROUND-COLOR: white; TEXT-DECORATION: none">
<TD>
<s:property value="#cust.cust_name" />
</TD>
</s:iterator>
-----------------------沒有var的會(huì)將循環(huán)后取出的對(duì)象壓在root中
<%-- <s:iterator value="#list" >
<TR
style="FONT-WEIGHT: normal; FONT-STYLE: normal; BACKGROUND-COLOR: white; TEXT-DECORATION: none">
<TD>
<s:property value="cust_name" />
</TD>
</s:iterator>
--------整體使用規(guī)則類似在java中的使用










