struts2_8_接受显示层传递的参数

时间:2014-08-24 10:21:12   收藏:0   阅读:296

struts.xml文件的配置:

<struts>
	<package name="package" namespace="/test" 
  extends="struts-default">
		<action name="emp*" class="struts.employeeAction" 
  method="{1}">
			<result name="success">/show.jsp</result>
		</action>
	</package>
</struts>

(一)采用基本类型接受参数(get/post:

Action类型中定义与请求参数同名的属性,struts2便能自动接受请求参数并赋予给同名属性。

1Action中的代码:

   private Integer id;
	private String name;

	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 execute() {
		return "success";
	}

2)输入显示层的代码:

<body>
  <form action="<%=request.getContextPath()%>/test/emp" 		
  method="post">
  id  : <input type="text" name="id" /><br />
  name: <input type="text" name="userName"><br />
  <input type="submit" value="提交" />
  </form>
</body>

3)输出显示层的代码:

 <body>
	id   =  ${id } <br>
	name =  ${userName }
</body>

请求路径:http://localhost:8080/Struts_3/index.jsp

(二)复合类型接收请求参数:(推荐使用)

1Action中的代码:

public class employeeAction {
	private Person person;

	public Person getPerson() {
		return person;
	}

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

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

2Person类:

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

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

}

3)输入层的代码:

<body>
	<form action="<%=request.getContextPath()%>
      /employee/employee.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>

4)输出层的代码:

  <body>
    id   = ${person.id }<br>
    name = ${person.name }
  </body>

请求路径:http://localhost:8080/Struts_3/index.jsp











原文:http://blog.csdn.net/u012963457/article/details/38793725

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!