例子:
RegistAction的代碼:
package com.wss.action; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.wss.Dao.School; import com.wss.Dao.User; import com.wss.Dao.UserDao; public class RegistAction extends ActionSupport { public RegistAction() { System.out.println("Initialization RegistAction...."); } //user有Get和Set方法,是類成員,只需要賦值就可以 private User user =new User(); //private User user; public User getUser() { System.out.println("Getting the getUser"); return user; } public void setUser(User user) { System.out.println("Setting the setUser"); this.user = user; } //school有Get和Set方法,是類成員,只需要賦值就可以 private School school; public School getSchool() { System.out.println("Getting the getSchool"); return school; } public void setSchool(School school) { System.out.println("Setting the setSchool"); this.school = school; }
//company有Get和Set方法,是類成員,只需要賦值就可以 private String company; public void setCompany(String company) { System.out.println("Setting the company"); this.company=company; } public String getCompany() { return this.company; } public String execute() throws Exception{ UserDao ud =new UserDao(); //ActionContext ctx = ActionContext.getContext(); //HttpServletRequest request = (HttpServletRequest) ctx.get(org.apache.struts2.StrutsStatics.HTTP_REQUEST); //request.setAttribute("company", this.company); System.out.println("The company is "+this.company+" The name is "+this.user.getName()+" The address is "+this.user.getAddress()); System.out.println("The school name is "+this.school.getName()+" The city is "+this.school.getCity()+" The department is "+ this.school.getDepartment());
ActionContext.getContext().put("message","注冊成功"); ServletActionContext.getRequest().setAttribute("school","北京大學"); //if(ud.regist(user)!=0){ ActionContext.getContext().getSession().put("welcome", "歡迎訪問"); ServletActionContext.getRequest().getSession().setAttribute("city", "北京,上海,深圳"); String label="標簽內容"; ActionContext.getContext().put("label", label); this.addFieldError("success", "成功"); return SUCCESS; //} //this.addFieldError("error", "注冊失敗"); //return ERROR; } }
regist.jsp代碼:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <%@ page contentType="text/html;charset=gbk"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>京東商城注冊頁面</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <% request.setCharacterEncoding("gbk"); %> </head> <body> <center> <form action="regist" method="post"> 用戶名:<input type="text" name="user.name"/><br> 密 碼:<input type="password" name="user.password"/><br> 手 機:<input type="text" name="user.phone" /><br> 地 址:<input type="text" name="user.address"/><br> 公 司: <input type="text" name="company"/> <br> 學 校:<input type="text" name="school.name"/> 城 市:<input type="text" name="school.city" /> 院 系:<input type="text" name="school.department" /> <table> <tr> <td><input type="submit" value="注冊"/></td> <td><input type="reset" value="重置" ></td> </tr> </table> </form> <s:fielderror /> </center> </body> </html>
login.jsp代碼:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>京東商城</title> <style> .head ul{ width:980px; border:1px; solid:#000; margin:0 auto; } .head ul li{ float:left; } .head ul li a{ width:80px;/*設置元素寬為80px*/ height:28px;/*設置高度為28px*/ line-height:28px;/*設置行距為28px,讓文字在每行的中間位置*/ background:#3A5FCD;/*設置元素的背景為紅色*/ color:#FFF;/*文字顏色是白色*/ margin:5px 10px; font-size:12px;/*用12號字*/ display:block;/*這個比較關鍵,因為a本身就是聯級元素,本身不具有寬高,用這個把它變成塊級元素,這樣前面設置的寬和高就能起作用了*/ text-align:center;/*讓文本居中*/ text-decoration:none; /*去掉下划線*/ } .head ul li a:hover{ /*這個大概的意思就是當鼠標放到這個a元素的上面時,這個a元素的樣式就按下面的代碼執行*/ width:78px; height:26px; line-height:28px; border:1px solid red; color:white; background:#40E0D0; } </style> <script type="text/javascript"> function forword(){ window.location.href="regist.jsp"; } </script> </head> <body> <center> <div class="head"> <ul> <li><a href="login.jsp">首頁</a></li> <li><a href="">商品</a></li> <li><a href="">用戶信息</a></li> <li><a href="">購物車</a></li> <li><a href="">發現</a></li> <li><a href="">請聯系我們</a></li> </ul> </div> <h2><font color="#FF7F00">登錄</font></h2> <form action="login" method="post" name="myform"> 用戶名:<input type="text" name="name" /><br> 密 碼:<input type="password" name="password"/><br> <table> <tr> <td><input type="submit" value="登錄"/></td> <td><input type="reset" value="重置" ></td> <td><input type="button" value="注冊" onClick="forword()"></td> </tr> </table> </form> <s:fielderror> <s:param>success</s:param> </s:fielderror> 類屬性 company(el表達式): ${company}</br> 類屬性 用戶名 (el表達式):${user.name}</br> 方法值 request 注冊 (el表達式):${message }</br> 方法值 session 歡迎詞 (el表達式) :${welcome}</br> 方法值 session 城市 (el表達式):${city}</br> struts 類屬性 用戶名:<s:property value="user.name"/></br> struts 類屬性 公司:<s:property value="company"/></br> struts session welcome:<s:property value="welcome"/> </br> struts session 城市:<s:property value="city"/></br> struts 方法值 request 標簽:<s:property value="label"/></br> struts 類屬性 school: <s:property value="school.city"/> </br> 方法值 request 學校 (el表達式):${school} </br> 方法值 request 標簽:${label} </center> </body> </html>
運行結果:
注意:
(1)發現el表達式不管是通過:ActionContext.getContext().put("message",message);
ServletActionContext.getRequest().setAttribute("messae",message);把類屬性數據或者方法數據存儲到request中,都能用el表達式獲得。
(2)方法中的數據值用:
ActionContext.getContext().getSesstion().put("message",message);
ServletActionContext.getRequest().getSesstion().setAttribute("message",message);這兩種方法把數據存儲到session中,都可以用el表達式獲得,並且不需
要${sessionScope.message }中的sessionScope也可以獲取數據。
(3)同時struts標簽可以對類屬性值(自帶有set和get就是默認會放入request中)、沒有set和get的類屬性值編寫代碼放入request中、方法中的值用代碼放入request中
都可以用struts自帶的標簽<s:property value="message"/>獲取數據。
但是struts標簽對放入session中的數據不能顯示。
如果打開瀏覽器重新輸入http://localhost:8080/ShopDemo/,那么用session保存的數據仍然存在。
瀏覽器同一個標簽,但是前進和退回,在注冊頁不輸入任何的內容,但是當點擊注冊時候,用ActionContext,ServletActionContext的方法通過request或者session的方法保存的數據,仍然存在;但是通過struts2自動將action的所有帶有get,set(這兩個方法必須同時有)的屬性放入request域中的數據,沒有了,顯示空值。
request 和sesstion保存數據的作用域,時間域,區別和聯系?