1. 程式人生 > >struts2訪問web元素集合

struts2訪問web元素集合

java程式碼:

package Test.Action.UserAction;


import java.util.Map;


import javax.servlet.http.HttpServletRequest;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


@SuppressWarnings("serial")
public class UserAction extends ActionSupport{

private Map request;
private Map session;
private Map application;

public  UserAction(){

request=(Map)ActionContext.getContext().get("request");
session=ActionContext.getContext().getSession();
application=ActionContext.getContext().getApplication();
}

public String execute(){

request.put("r","r1");
session.put("s","s1");
application.put("a","a1");

return SUCCESS;
}

public Map getRequest() {
return request;
}


public void setRequest(Map request) {
this.request = request;
}


public Map getSession() {
return session;
}


public void setSession(Map session) {
this.session = session;
}


public Map getApplication() {
return application;
}


public void setApplication(Map application) {
this.application = application;
}


}

-------------------------------------------------------------------------------------------------------------------------------------------

struts.xml程式碼:

<constant name="struts.devMode" value="true" />
    <package name="user" namespace="/" extends="struts-default">
    <action name="user" class="Test.Action.UserAction.UserAction">
    <result>/welcome.jsp</result>
    </action>
    </package>

-------------------------------------------------------------------------------------------------------------------------------------------

jsp程式碼:

 request、session、與application:<br/> 
  
  <hr>
    El表示式取值:<br/>
    request.r=${request.r}<br>
    session.s=${session.s}<br>
    application.a=${application.a}<br>
    <hr>
    S標籤的property屬性取值:<br/>
    request.r=<s:property value="#request.r"/><br/>
    session.s=<s:property value="#session.s"/><br>
    application.a=<s:property value="#application.a"/><br>
     <hr>
   S標籤的property屬性取值(第二種方法):<br/>
    attr.r=<s:property value="#attr.r"/><br/>
    attr.s=<s:property value="#attr.s"/><br/>
    attr.a=<s:property value="#attr.a"/><br/>
    <hr>
    java程式碼取值:<br/>
    request.getAttribute("r")=<%=request.getAttribute("r") %><br>
    session.getAttribute("s")=<%=session.getAttribute("s") %><br>
    application.getAttribute("a")=<%=application.getAttribute("a") %><br>
    <hr>
    <s:debug></s:debug>
    
結果如圖所示: