struts2之请求参数接收

时间:2020-12-01 08:58:15

struts2之请求参数接收

1. 采用基本类型接受请求参数(get/post)
在Action类中定义与请求参数同名的属性,struts2便能自动接收请求参数并赋予给同名的属性。
请求路径:http://localhost:8080/action/register.action?id=33


publicclass HelloWorldAction {
private Integer id; public Integer getId() {
return id;
} publicvoid setId(Integer id) {
this.id = id;
}
}

2. 采用复合类型接受请求参数
请求路径:http://localhost:8080/action/register.action?id=79


publicclass HelloWorldAction {
private Person person; public Person getPerson() {
return person;
} publicvoid setPerson(Person person) {
this.person = person;
} }

获取Bean属性的原理:
Struts2首先通过反射技术调用Person的默认构造器创建person的实例,然后再通过反射技术调用person中与请求参数同名的属性的setter方法,来获取请求参数数值。

实体bean


package com.ljq.bean;

publicclass Person {
private String name;
private Integer id; public String getName() {
return name;
} publicvoid setName(String name) {
this.name = name;
} public Integer getId() {
return id;
} publicvoid setId(Integer id) {
this.id = id;
} }

HelloWorldAction类


package com.ljq.action;

import com.ljq.bean.Person;

publicclass HelloWorldAction {
//基本类型请求参数
private Integer id;
private String name;
//复合类型请求参数
private Person person; public String execute(){
return"success";
} public Person getPerson() {
return person;
} publicvoid setPerson(Person person) {
this.person = person;
} public Integer getId() {
return id;
} publicvoid setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} publicvoid setName(String name) {
this.name = name;
} }

index.jsp请求页面


<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="">
</head> <body>
<!-- get方法发送,如果是中文则会出现乱码,post不会,好像struts2..8有修正此问题
基本类型get请求参数: http://localhost:8083/struts2/control/employee/executeHelloWorld.do?id=1&name= zhangsan
复合类型get请求参数: http://localhost:8083/struts2/control/employee/executeHelloWorld.do?person.id=2&person.name=lisi http://localhost:8083/struts2/control/employee/executeHelloWorld.do?id=1&name=zhangsan&person.id=2&person.name=lisi
--> <!-- post方法发送 -->
<form action="<%=request.getContextPath()%>/control/employee/executeHelloWorld.do" method="post">
<!-- 基本类型请求参数 -->
id:<input type="text" name="id"><br/>
name:<input type="text" name="name"><br/>
<!-- 复合类型请求参数 -->
person.id:<input type="text" name="person.id"><br/>
person.name:<input type="text" name="person.name"><br/>
<input type="submit" value="发送"/>
</form>
</body>
</html>

显示页面


<%@ page language="java"import="java.util.*,java.net.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head> <title>My JSP 'message.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="">
</head> <body>
<!-- 输出基本类型请求参数 -->
id=${id}<br/>
name=${name}<br/>
<!-- 输出复合类型请求参数 -->
person.id=${person.id }<br/>
person.name=${person.name }<br/>
</body>
</html>

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>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.action.extension" value="do,go"/> <package name="employee" namespace="/control/employee"extends="struts-default">
<action name="*HelloWorld"class="com.ljq.action.HelloWorldAction" method="{1}">
<result name="success">/WEB-INF/page/message.jsp</result>
</action>
</package>
</struts>