struts2 请求参数接收

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

public class HelloWorldAction {
private Integer id;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}
}

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

public class HelloWorldAction {
private Person person;

public Person getPerson() {
return person;
}

public void setPerson(Person person) {
this.person = person;
}

}

 

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

实体bean

package com.ljq.bean;

public class Person {
private String name;
private Integer id;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

}

 HelloWorldAction类

package com.ljq.action;

import com.ljq.bean.Person;

public class HelloWorldAction {
//基本类型请求参数
private Integer id;
private String name;
//复合类型请求参数
private Person person;


public String execute(){
return "success";
}


public Person getPerson() {
return person;
}

public void setPerson(Person person) {
this.person = person;
}

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;
}

}

 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="0">
</head>

<body>
<!-- get方法发送,如果是中文则会出现乱码,post不会,好像struts2.1.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="0">
</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>

 

posted @ 2014-12-14 23:03  _启明  阅读(241)  评论(0编辑  收藏  举报