struts2提供了兩種請求參數(shù)的接受方式
文章使用的配置文件
<struts>
<constant name="struts.i18n.encoding" value="UTF-8" />
<package name="Action" namespace="/" extends="struts-default">
<action name="execute" class="Action.testAction">
<result>/execute.jsp</result>
</action>
</package>
</struts>
- 采用基本類型接受請求參數(shù)(get/post)
這種是最基本的方式送膳,就是在action中定義相應(yīng)的屬性和方法并實現(xiàn)getter()和setter()方法來接受鹰服。
請求路徑:http://localhost:8080/testStruts/execute?id=1
public class testAction extends ActionSupport{
private int id;
public String execute() throws Exception {
return "success";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
- 采用復(fù)合類型接受請求參數(shù)
請求路徑:http://localhost:8080/testStruts/execute?person.name=1
public class testAction extends ActionSupport{
private Person person;
public String execute() throws Exception {
return "success";
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
使用反射技術(shù)轩拨,先去調(diào)用默認(rèn)構(gòu)造函數(shù),然后調(diào)用相應(yīng)的getter()和setter()方法牺陶,所以一定要有一個默認(rèn)的構(gòu)造函數(shù)
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person() {
}
}