Struts2中接受参数(一):
StrudentAction.java:
package com.oristand.test;
import com.opensymphony.xwork2.ActionSupport;
public class StudentAction extends ActionSupport {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
public String add(){
System.out.println("name is: " + name );
System.out.println("age is: " + age );
return SUCCESS;
}
public String delete(){
return SUCCESS;
}
}
Struts.xml:
<package name="default" namespace="/" extends="struts-default">
<action name="*_*" class="com.oristand.test.{1}Action" method="{2}" >
<result>/{1}_{2}.jsp</result>
</action>
</package>
客户端访问:
http://localhost:8888/struts2_0100_ActionConfig/Student_add?name=gaoge&age=24
则在console下打印出来:
name is: gaoge
age is: 24
Struts2中接受参数(二):利用domainModel
StudentAction.java
package com.oristand.test;
import com.opensymphony.xwork2.ActionSupport;
import com.oristand.vo.Person;
public class StudentAction extends ActionSupport {
private String name;
private int age;
private Person person;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
public String add(){
System.out.println("name is: " + name );
System.out.println("age is: " + age );
System.out.println("person's name is: " + person.getName() );
System.out.println("person's age is: " + person.getAge() );
return SUCCESS;
}
public String delete(){
return SUCCESS;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
Person.java:
package com.oristand.test;
import com.opensymphony.xwork2.ActionSupport;
import com.oristand.vo.Person;
public class StudentAction extends ActionSupport {
private String name;
private int age;
private Person person;//这个person不需要new ,struts2会自动new 出来一个
public String getName() {
return name;
}