一、訪問servelt API:html
在struts1.x Action類的execute方法中,有四個參數,其中兩個就是response和request。而在Struts2中,並無任何參數,所以,就不能簡單地從execute方法得到HttpServletResponse或HttpServletRequest對象了。
但在Struts2 Action類中仍然有不少方法能夠得到這些對象。下面就列出四種得到這些對象的方法。 java
【方法1】使用Struts2 Aware攔截器 :apache
package action; import com.opensymphony.xwork2.ActionSupport; import javax.servlet.http.*; import org.apache.struts2.interceptor.*; public class MyAction extends ActionSupport implements ServletResponseAware { private javax.servlet.http.HttpServletResponse response; // 得到HttpServletResponse對象 public void setServletResponse(HttpServletResponse response) { this.response = response; } public String execute() throws Exception { response.getWriter().write("實現ServletResponseAware接口"); } }
【方法2】使用RequestAware攔截器 json
package action; import java.util.Map; import org.apache.struts2.*; import com.opensymphony.xwork2.ActionSupport; import javax.servlet.http.*; import com.opensymphony.xwork2.util.*; import org.apache.struts2.interceptor.*; public class FirstAction extends ActionSupport implements RequestAware { private Map request; private HttpServletResponse response; public void setRequest(Map request) { this.request = request; } public String execute() throws Exception { java.util.Set<String> keys = request.keySet(); // 枚舉全部的key值。實際上只有一個key:struts.valueStack for(String key: keys) System.out.println(key); // 得到OgnlValueStack 對象 OgnlValueStack stack = (OgnlValueStack)request.get("struts.valueStack"); // 得到HttpServletResponse對象 response = (HttpServletResponse)stack.getContext().get(StrutsStatics.HTTP_RESPONSE); response.getWriter().write("實現RequestAware 接口"); } }
方法3】使用ActionContext類 session
HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE); HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
【方法4】使用ServletActionContext類 ide
Struts2爲咱們提供了一種最簡單的方法得到HttpServletResponse及其餘對象。這就是org.apache.struts2.ServletActionContext類。咱們能夠直接使用ServletActionContext類的getRequest、getResponse方法來得到HttpServletRequest、HttpServletResponse對象。代碼以下: 測試
HttpServletResponse response = ServletActionContext.getResponse() response.getWriter().write("hello world");
一、ActionContext獲取request,session,值棧。將對象壓人棧頂:獲取值棧,push。<s:屬性>:是OGNL,獲取棧頂的對象。this
二、<result type="json" name="users">
<param name="root">names</param>
</result>spa
三、(1)、 request.setAttribute("s004po", s004po);code
主題:${s004po.ma002}<br>
內容:${s004po.ma003}
(2)、List<Map<String, Object>> replyList
request.setAttribute("replyList", replyList);
<s:iterator value="#request.replyList" id="column">
<s:iterator value="#column" id="map">
key: <s:property value="#map.key"/>
value: <s:property value="#map.value"/> <br>
</s:iterator>
</s:iterator>
四、hidden標籤中的value我在js腳本中取得hidden中value時爲何獲得的是<s:property value="#session.LOGIN_USER"/>,我用<s:textfield value="<s:property value='#session.LOGIN_USER'/>"/>測試時在文本框中的顯示也是<s:property value="#session.LOGIN_USER"/>,求教…………
解答:
<s:hidden>標籤的value屬性的類型是String類型,因此把<s:property value="#session.LOGIN_USER"/>當成是字符串,若是要獲取值棧中的數據要用
<s:hidden name="loginUser" id="loginUser" value="%{#session.LOGIN_USER}"/>
http://lj6684.iteye.com/blog/1002279
http://blog.sina.com.cn/s/blog_87720b870100zkn2.html
struts2上傳文件問題彙總
http://jingyan.baidu.com/article/acf728fd1222d8f8e510a38e.html
http://localhost:8080/YunVideo/pda/pdaLogin
http://localhost:8080/YunVideo/pda/pdaRegister
<action name="pda*" class="com.hw.hwsafe.pda.action.PdaUserAction" method="pda{1}">
<result type="json"></result>
</action>
public void pdaRegister() throws Exception { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter out = null; out = response.getWriter(); out.write("註冊"); out.flush(); out.close(); } public void pdaLogin() throws Exception { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter out = null; out = response.getWriter(); out.write("登陸"); out.flush(); out.close(); }