两种方式:
1.在action 中定义与请求参数同名的属性,并提供相应的getter,setter method
CompanyAction.java
----------------------------------------
package org.taink.struts.action;
public class CompanyAction {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String doAdd() {
return "success";
}
public String execute() {
return "success";
}
}
index.jsp
--------------------------------
<body>
<form action="control/company/list_doAdd" method="post">
id:<input type="text" name="id"><br/>
name:<input type="text" name="name"><br/>
<input type="submit" value="提交">
</form>
</body>
message.jsp
-----------------------------------------
<body>
departmentId:${id}<br/>
departmentName: ${name}<br/>
</body>
2.采用复合类型接收请求参数
CompanyAction.java
----------------------------------------
package org.taink.struts.action;
import org.taink.entity.Department;
import org.taink.entity.Employee;
// struts2 采用无入侵设计(即不依赖其它框架,类,jar的支持)
public class CompanyAction {
private Department department;
private Employee employee;
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public String doAdd() {
return "success";
}
public String execute() {
return "success";
}
}
index.jsp
--------------------------------
<body>
<form action="control/company/list_doAdd" method="post">
departmentId:<input type="text" name="department.deptId"><br/>
departmentName: <input type="text" name="department.deptName"><br/>
employeeId: <input type="text" name="employee.empId"><br/>
employeeName: <input type="text" name="employee.empName"><br/>
<input type="submit" value="提交">
</form>
</body>
message.jsp
-----------------------------------------
<body>
departmentId:${department.deptId}<br/>
departmentName: ${department.deptName}<br/>
employeeId: ${employee.empId}<br/>
employeeName: ${employee.empName}<br/>
</body>
-----------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------
struts.xml
--------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 该属性设置struts2是否支持动态方法调用,该值默认为:true,如果要关闭动态方法调用,则可以设置为:false -->
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<package name="taink" namespace="/control/company" extends="struts-default">
<action name="list_*" method="{1}"
class="org.taink.struts.action.CompanyAction">
<result name="success">/WEB-INF/page/message.jsp</result>
</action>
</package>
</struts>