struts.xml文件的配置:
<struts>
<package name="package" namespace="/test"
extends="struts-default">
<action name="emp*" class="struts.employeeAction"
method="{1}">
<result name="success">/show.jsp</result>
</action>
</package>
</struts>
(一)采用基本类型接受参数(get/post):
在Action类型中定义与请求参数同名的属性,struts2便能自动接受请求参数并赋予给同名属性。
1)Action中的代码:
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 execute() {
return "success";
}
2)输入显示层的代码:
<body>
<form action="<%=request.getContextPath()%>/test/emp"
method="post">
id : <input type="text" name="id" /><br />
name: <input type="text" name="userName"><br />
<input type="submit" value="提交" />
</form>
</body>
3)输出显示层的代码:
<body>
id = ${id } <br>
name = ${userName }
</body>
请求路径:http://localhost:8080/Struts_3/index.jsp
(二)复合类型接收请求参数:(推荐使用)
1)Action中的代码:
public class employeeAction {
private Person person;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public String execute() {
return "success";
}
}
2)Person类:
public class Person {
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;
}
}
3)输入层的代码:
<body>
<form action="<%=request.getContextPath()%>
/employee/employee.action" method="post">
id: <input type="text" name="person.id"/><br/>
name:<input type="text" name="person.name"><br/>
<input type="submit" value="提交"/>
</form>
</body>
4)输出层的代码:
<body>
id = ${person.id }<br>
name = ${person.name }
</body>
请求路径:http://localhost:8080/Struts_3/index.jsp