程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> Struts2-尚學堂馬士兵-學習隨心記

Struts2-尚學堂馬士兵-學習隨心記

編輯:關於JSP

學習隨心記

Struts標簽定義taglib

 

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

 

Struts.xml中設置開發模式devmode

 

 

Struts.xml中設置默認編碼encoding

 

 

Struts.xml中導入包含其他模塊

 

 

Action中標記錯誤

 

this.addFieldError(error_name,The Name Is Not Equals To qsuron);
return ERROR;

Struct.xml中設置默認Default Action

 

 

Jsp中取出錯誤信息

 


 

也可以用debug進行查看

result類型

 

1.dispatcher 服務器端的跳轉(URL不變)
2.redirect 客戶端的跳轉(URL改變,重新請求新的頁面)
3.chain 能跳轉到action
4.redirectAction 能跳轉到action
5.freemaker 一個框架
6.httpheader HTTP頭信息
7.stream 上傳下載
8.xslt 和XML相關的修飾語言
9.plaintext 返回源碼(帶<%的,做教程網站的話可能會用到)
10.title 動態更新網頁標題

 

 




 

 
	
		
        
            /ActionIntroduction.jsp
        
        
            /r1.jsp
        
        
            /r2.jsp
        
        
            r1
        
        
            r2
        
	
    


常見問題,當要chain的其他action位於其他的namespace時,代碼如下:

 


        dashboard/secure
        
    


 


 

 

 

Action裡操作客戶端request,session,application-方式1

 

package com.qsuron.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction1 extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private Map request;
	private Map session;
	private Map application;

	@SuppressWarnings(unchecked)
	public LoginAction1(){
		request = (Map) ActionContext.getContext().get(request);
		session = ActionContext.getContext().getSession();
		application = ActionContext.getContext().getApplication();
	}
	
	public String login(){
		request.put(r1,request-qsuron);
		session.put(s1,session-qsuron);
		application.put(a1,application-qsuron);
		return SUCCESS;
	}
}

Action裡操作客戶端request,session,application-方式2-最常用的

 

package com.qsuron.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.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**最常用
 * 體現了DI(dependcy injection)或者說是IoC(inverse of control)設計思想
 * 類似於DomainModelParamInput
 */
public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
	private static final long serialVersionUID = 1L;
	private Map request;
	private Map session;
	private Map application;

	public String login(){
		System.out.println(222);
		request.put(r1,request-qsuron);
		session.put(s1,session-qsuron);
		application.put(a1,application-qsuron);
		return SUCCESS;
	}

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

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

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

 

Action裡操作客戶端request,session,application-方式3
package com.qsuron.action;

import java.util.Map;

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

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction3 extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private HttpSession session;
	private ServletContext application;

	public LoginAction3(){
		request = ServletActionContext.getRequest();
		session = request.getSession();
		application = session.getServletContext();
	}
	
	public String login(){
		System.out.println(333);
		request.setAttribute(r1,request-qsuron);
		session.setAttribute(s1,session-qsuron);
		application.setAttribute(a1,application-qsuron);
		return SUCCESS;
	}
}
Action裡操作客戶端request,session,application-方式4

 

 

package com.qsuron.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 LoginAction4 extends ActionSupport implements ServletRequestAware{
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private HttpSession session;
	private ServletContext application;

	public String login(){
		System.out.println(444);
		request.setAttribute(r1,request-qsuron);
		session.setAttribute(s1,session-qsuron);
		application.setAttribute(a1,application-qsuron);
		return SUCCESS;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		this.session = request.getSession();
		this.application = session.getServletContext();
	}


}

 

客戶端使用Struts2或者Jsp語句對action中的request,session,application進行取值

 


<%=request.getAttribute(r1) %>

 

 

 


 

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