1. 采用基本类型接受请求参数(get/post)
在Action类中定义与请求参数同名的属性,struts2便能自动接收请求参数并赋予给同名的属性。请求路径:public class HelloWorldAction {private Integer id;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}}
2. 采用复合类型接受请求参数
请求路径: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"%>My JSP 'index.jsp' starting page
显示页面
<%@ page language="java" import="java.util.*,java.net.*" pageEncoding="UTF-8"%>My JSP 'message.jsp' starting page id=${id}name=${name} person.id=${person.id }person.name=${person.name }
struts.xml配置文件
/WEB-INF/page/message.jsp