1. 程式人生 > >struts2[2.3]引數獲得方式-(1)屬性驅動獲得引數

struts2[2.3]引數獲得方式-(1)屬性驅動獲得引數

1.學習路線

今天咱們來學struts2引數獲得方式,let`go!

                                                                                           圖1.學習路線

                                                                                           圖2.類和配置檔案

form1.jsp,寫一個表單,用於提交資料:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
		<form action="${pageContext.request.contextPath}/Demo8Action">
			使用者名稱:<input type="text" name="name" /><br>
			年齡:<input type="text" name="age" /><br>
			生日:<input type="text" name="birthday" /><br>
			<input type="submit" value="提交" />
		</form>
</body>
</html>

2.屬性驅動獲得引數

新建一個Demo8Action類,繼承ActionSupport,再建立一個execute()方法,return SUCCESS。

package cn.aisino.c_param;

import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
//struts2如何獲得引數
//每次請求Action時都會建立新的Action例項物件
public class Demo8Action extends ActionSupport {
	
        //呼叫空參構造方法
	public Demo8Action(){
		super();
		System.out.println("Demo8Action被建立了!");
	}
	
	//準備與引數鍵名稱相同的屬性
	private String name;
	//自動型別轉換,只能轉換8大基本資料型別以及包裝型別
	private Integer age;
	//支援特定型別字串轉換成Date,例如yyyy-MM-dd
	private Date birthday;
	
	public String execute() throws Exception {
	
		System.out.println("name:" + name + ",age:"+age+",生日:"+birthday);
		return SUCCESS;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}
	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
}

    生成getters()和setters()方法,

    在包下的配置檔案struts.xml中配置Action,

    在主配置檔案struts.xml中配置,

    啟動伺服器,在位址列中訪問Demo8Action:

    提交,控制檯可檢視資料成功提交: