struts訪問Servlet API丶數據封裝

1. 訪問Servlet API

1.1 使用ActionContext類獲取Servlet API對象對應的Map對象

 ActionContext ac=ActionContext.getContext();                //獲取ActionContext對象
 Map<String,Object> request=(Map<String,Object>)ac.get("request");     //獲取請求的Map
 Map<String, Object> session=ac.getSession();                          //獲取session的Map
 Map<String, Object> application=ac.getApplication();                  //獲取application的Map
 session.put("key","value");                                           //把值放進session中

1.2 向Action注入Servlet API對象對應的Map對象

public class HelloAction implements RequestAware,SessionAware,ApplicationAware  {
    //定義3個Map<String,Object>屬性
    private Map<String, Object> request;
    private Map<String, Object> session;
    private Map<String, Object> application;
     
    //這3個方法必須實現,把注入的Servlet API賦值給上面的3個屬性,這樣整個類都可以使用
    public void setRequest(Map<String, Object> request) {
        this.request=request;  
    }
 
    public void setSession(Map<String, Object> session) {
        this.session=session;
    }
 
    public void setApplication(Map<String, Object> application) {
        this.application=application;
    }

1.3 向Action注入原生態Servlet API

//實現2個接口
public class HelloAction implements  ServletRequestAware,ServletResponseAware {
    //定義Servlet API的請求request和響應response
    private HttpServletRequest request;
    private HttpServletResponse response;
     
    //實現2個方法實現注入,把注入的值賦值給屬性就可以在整個類中使用
    public void setServletRequest(HttpServletRequest request) {
        this.request=request;
         
    }
 
    public void setServletResponse(HttpServletResponse response) {
        this.response=response;
         
    }

1.4 使用ServletActionContext類訪問Servlet API

  • 推薦使用,代碼少,方便,能獲取所有Servlet API,但是耦合的,必須導入相應的Servlet包,功能完全跟原生態的Servlet API一樣
//獲取原生態的request
HttpServletRequest request=ServletActionContext.getRequest();
//獲取原生態的response
HttpServletResponse response=ServletActionContext.getResponse();
//根據request獲取原生態的session
HttpSession session=request.getSession();

2. 數據封裝

2.1 單個數據封裝

  • 例子參考2.2

2.2 對象數據封裝

  • 請求參數名稱必須以對象名稱爲前綴
  • 在Action聲明對象時不用顯式new對象,struts會自動創建的
  • 需要手動添加對象的getter和setter方法
<form action="/Struts02/addStudent" method="post">
	<!-- 單個請求參數,對name值沒要求 -->
	<input type="hidden" name="hidden_value" value="aaa"/>
	
	<!-- 對象請求參數name格式:對象名.屬性名 -->
	名字:<input type="text" name="student.name"/><br>
	性別:<select name="student.sex">
	<option value="1"></option>
	<option value="0"></option>
	</select><br>
	<input type="submit" value="提交"/>	
</form>
public class StudentAction extends ActionSupport{
	
	private String hidden_value;         //單個數據封裝,這裏的字段名要跟請求參數一致
	private Student student;             //封裝整個對象數據,請求參數要[student.屬性]才能封裝進對象中
	
	public String add(){
		
		System.out.println(hidden_value);  //直接獲取請求參數值
		System.out.println(student.getName());  //把請求參數值封裝進對象中後根據對象屬性獲取值
		System.out.println(student.getSex());
		return SUCCESS;
	}
		
	
	/* 無論封裝單個數據或封裝對象都要設置getter和setter方法 */
	public void setHidden_value(String hidden_value) {
		this.hidden_value = hidden_value;
	}

	public String getHidden_value() {
		return hidden_value;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public Student getStudent() {
		return student;
	}
}

2.3 使用模型驅動ModelDriven接口

  • 請求參數名稱不需要前綴,只要跟對象字段屬性一致即可
  • 在聲明對象時必須new一個新對象,不然會報空指針異常
  • 一個Action只能實現一個ModelDriven接口,所以需要封裝多個數據對象時該方式不適用,要用2.2方式
<form action="/Struts02/addStudent" method="post">				
		<!-- 模型驅動ModelDriven對請求參數name不要求帶前綴,只要name值跟對象的字段值一致即可 -->
		名字:<input type="text" name="name"/><br>
		性別:<select name="student.sex">
		<option value="1"></option>
		<option value="0"></option>
		</select><br>
		<input type="submit" value="提交"/>		
	</form>
//實現ModelDriven泛型接口
public class StudentAction extends ActionSupport implements ModelDriven<Student>{
	
	private Student student=new Student();  //這裏必須new一個新對象
	
	//重寫ModelDriven接口的方法
	@Override
	public Student getModel() {	
		//把student對象返回即可
		return student;
	}

	public String add(){
		
		//把請求參數值封裝進對象中後根據對象屬性獲取值
		System.out.println(student.getName());  
		System.out.println(student.getSex());
		return SUCCESS;
	}

2.4 封裝集合數據

<form action="/Struts02/addStudent2" method="post">	
		<!-- 格式:集合名稱[下標].屬性 -->				
		名字:<input type="text" name="studentList[0].name"/><br>
		班級:<input type="text" name="studentList[0].grade.name"/><br>
		名字:<input type="text" name="studentList[1].name"/><br>
		班級:<input type="text" name="studentList[1].grade.name"/><br>
		名字:<input type="text" name="studentList[2].name"/><br>
		班級:<input type="text" name="studentList[2].grade.name"/><br>
		<input type="submit" value="提交"/>	
	</form>
private List<Student> studentList;   //聲明對象集合屬性
	
	public String add(){
		//讀取對象集合中的屬性
		for (Student student : studentList) {
			System.out.println(student.getName()+","+student.getGrade().getName());
		}	
		return SUCCESS;
	}

	//設置getter和setter方法
	public List<Student> getStudentList() {
		return studentList;
	}

	public void setStudentList(List<Student> studentList) {
		this.studentList = studentList;
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章