src/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.enable.DynamicMethodInvocation" value="false"/> <constant name="struts.action.extension" value="do,action"/> <package name="employee" namespace="/control/employee" extends="struts-default"> <action name="list_*" class="cn.itcast.action.HelloWorldAction" method="{1}"> //method="{1}"接收action通配符传来的值 <result name="success">/WEB-INF/page/message.jsp</result> </action> </package> </struts>
输入id和name发送到action,通过反射技术调用与请求参数同名属性
的setter方法获取请求参数值
WebRoot/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> <form action="<%=request.getContextPath()%>/control/employee/list_execute.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> </html>
package cn.itcast.action; import cn.itcast.bean.Person; public class HelloWorldAction { private Integer id; private String name; private Person person; 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; } public String addUI(){ return "success"; } public String execute() throws Exception{ return "success"; } }
<%@ page language="java" import="java.util.*" 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=${person.id }<br/> name=${person.name } </body> </html>