7、请求参数接收
对与请求参数,可以在所请求的action中添加相应的属性,写出get和set方法,在表单中配置name属性与action中属性的名称一致,提交到所在action即可;如下:
12345678910111213141516171819public
class
HelloWorldAction {
private
String name;
private
int
id;
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
int
getId() {
return
id;
}
public
void
setId(
int
id) {
this
.id = id;
}
}
则index.jsp中表单中应如下(对应属性名一致):
12345<
form
action
=
"${pageContext.request.contextPath }/control/department/list_execute.action"
method
=
"post"
>
<
input
type
=
"text"
name
=
"id"
><
br
>
<
input
type
=
"text"
name
=
"name"
><
br
>
<
input
type
=
"submit"
value
=
"提交"
>
</
form
>
获取值:12${id }</
br
>
${name }
对于复杂请求参数(以对象封装,实际应用如此;Person类必须使用默认构造器,不能自己添加构造器):
1234567891011public
class
HelloWorldAction {
private
Person person;
public
Person getPerson() {
return
person;
}
public
void
setPerson(Person person) {
this
.person = person;
}
}
则表单如下:
12345<
form
action
=
"${pageContext.request.contextPath }/control/department/list_execute.action"
method
=
"post"
>
<
input
type
=
"text"
name
=
"person.id"
><
br
>
<
input
type
=
"text"
name
=
"person.name"
><
br
>
<
input
type
=
"submit"
value
=
"提交"
>
</
form
>
获取的值的方法如下:
12${person.id }</
br
>
${person.name }
- struts2.1.6版本存在中文请求参数乱码,可以自己添加过滤器;