action接受前台参数—属性接收和实体模型接收

[b]struts2中的action接收客户端页面传递过来的数据[/b]
在此例中使用了属性接收和domain Model的类型接收。在使用domain Model接收参数的时候要注意JSP页面<input/>标签中name的值要写成 实体.实体属性 的这种形式,否则action中接收不到该对象的属性值。具体示例代码如下:

客户端提交数据页面:clientCommitData.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>客户端提交数据的页面</title>

<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<form action="${pageContext.request.contextPath }/getParameter_getParameter" method="post">
id:<input type="text" name="id"/><br/>
name:<input type="text" name="name"/><br/>
birthday:<input type="text" name="birthday"/><br/>
age:<input type="text" name="person.age"/><br/>
sex:<input type="text" name="person.sex"/><br/>
<input type="submit" value="测试action接受客户端提交的数据"/>
</form>
</body>
</html>

显示用户提交的数据:showParameter.jsp


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>数据显示页面</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
在此页面显示另一个页面提交到action的数据<br/>
${id }-----id<br/>
${name }-----name<br/>
${birthday }----birthday<br/>
${person.age }-----person.age<br/>
${person.sex }-----person.sex
</body>
</html>


接收客户端提交数据的action:GetParameterAction.java

package com.sinwee.action;

import com.opensymphony.xwork2.ActionSupport;
import com.sinwee.domain.Person;

public class GetParameterAction extends ActionSupport {

/**
*
*/
private static final long serialVersionUID = -7145253382234265183L;

private String id;
private String name;
private String birthday;
private Person person;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}

public String getParameter() {

System.out.println (id + " 前台获取id");
System.out.println (name + " 前台获取name");
System.out.println (birthday + " 前台获取birthday");
System.out.println (this.person.getAge() + " 前台获取人员的age");
System.out.println (this.person.getSex() + " 前台获取人员的sex");

return "parameter";
}

}


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>
<package name="getParameter" namespace="" extends="struts-default">
<action name="getParameter_*" class="com.sinwee.action.GetParameterAction" method="{1}">
<result name="parameter">
/showParameter.jsp
</result>
</action>
</package>

<!-- Add packages here -->

</struts>



web.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值