程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> Struts2 訪問web元素

Struts2 訪問web元素

編輯:關於JSP

Struts2 訪問web元素


1、struts.xml

<struts>
 <constant name="struts.devMode" value="false"> 
 <constant name="struts.enable.DynamicMethodInvocation" value="true">
 <constant name="struts.i18n.encoding" value="utf-8"></constant>
 <package name="login" namespace="/login" extends="struts-default">                 
        <action name="login*" class="com.wxh.action.LoginActon{1}">
            <result>/useraddsuccess.jsp</result>            
        </action>         
  </package>     
</constant></constant></struts>

2、index.jsp
取得request,session,application的引用;<br />
<form action="" method="post" name="f">
	用戶名:<input name="name" type="text" /> 密碼:<input name="password" type="text" /><br />
	<input onclick="javascript:document,f,action='login/login1';document.f.submit();" type="button" value="submit1" /> <input onclick="javascript:document,f,action='login/login2';document.f.submit();" type="button" value="submit2" /> <input onclick="javascript:document,f,action='login/login3';document.f.submit();" type="button" value="submit3" /> <input onclick="javascript:document,f,action='login/login4';document.f.submit();" type="button" value="submit4" />&nbsp;</form>
3、useraddsuccess.jsp

<%@taglib uri="/struts-tags" prefix="s"%>


user login success!
| <%=request.getAttribute("r1") %>
| <%=session.getAttribute("s1") %>
| <%=application.getAttribute("a1") %>


4、LoginAction1.java
package com.wxh.action;

import java.util.Map; 
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginActon1 extends ActionSupport {
	private Map request;
	private Map session;
	private Map application;
	
	public LoginActon1(){
		request=(Map)ActionContext.getContext().get("request");
		session=ActionContext.getContext().getSession();
		application=ActionContext.getContext().getApplication();
	}
	public String execute(){
		request.put("r1", "r1");
		session.put("s1","s1");
		application.put("a1", "a1");
		return SUCCESS;
	}
}
LoginAction2.java
package com.wxh.action;

import java.util.Map; 

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class LoginActon2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Maprequest;
	private Mapsession;
	private Mapapplication;
	public String execute(){
		request.put("r1", "r1");
		session.put("s1","s1");
		application.put("a1", "a1");
		return SUCCESS;
	}
	@Override
	public void setApplication(Map application) {
		// TODO Auto-generated method stub
		this.application=application;
		
	}
	@Override
	public void setSession(Map session) {
		// TODO Auto-generated method stub
		this.session=session;
	}
	@Override
	public void setRequest(Map request) {
		// TODO Auto-generated method stub
		this.request=request;
		
	}
}

LoginAction3.java
package com.wxh.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class LoginActon3 extends ActionSupport{
	private HttpSession session;
	private HttpServletRequest request;
	private ServletContext application;
	
	public LoginActon3(){
		request=ServletActionContext.getRequest();
		session=request.getSession();
		application=session.getServletContext();
	}
	
	public String execute(){
		request.setAttribute("r1", "r1");
		session.setAttribute("s1","s1");
		application.setAttribute("a1", "a1");
		return SUCCESS;
	}

}

LoginAction4.java
package com.wxh.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

public class LoginActon4 extends ActionSupport implements ServletRequestAware{
	private HttpSession session;
	private HttpServletRequest request;
	private ServletContext application;	
	
	public String execute(){
		request.setAttribute("r1", "r1");
		session.setAttribute("s1","s1");
		application.setAttribute("a1", "a1");
		return SUCCESS;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request=request;
		this.session=request.getSession();
		this.application=session.getServletContext();
	}

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved