struts2 请求参数接收

一、get方式:

请求路径:localhost:8080/projectName/packageName/actionName.action?id=10&name=fuck

在HelloWorldAction.java 中加入两个成员变量 id 和 name  并生成相应的get和set方法

struts.xml文件中加入

<action name="income" class="cn.xs.action.HelloWorldAction" method = "execute">
	<!-- 配置返回结果 -->
	<result name="success">/WEB-INF/page/hello.jsp</result>
	<result name="error">/error.jsp</result>
</action>
在hello.jsp中加入
id=${id }<br>
name=${name }

访问localhost:8080/projectName/packageName/actionName.action?id=10&name=fuck 将会显示:

xxxxxxx id=23
name=fuck


二、post 方式:

struts.xml文件中加入

<action name="income" class="cn.xs.action.HelloWorldAction" method = "execute">
	<!-- 配置返回结果 -->
	<result name="success">/WEB-INF/page/hello.jsp</result>
	<result name="error">/error.jsp</result>
</action>
<action name="post_param" class="cn.xs.action.HelloWorldAction" method = "execute">
	<!-- 配置返回结果 -->
	<result name="success">/WEB-INF/page/hello.jsp</result>
	<result name="error">/error.jsp</result>
</action>
在hello.jsp代码如下:

<body>
	${message}    <br>xxxxxxx
	id=${id }<br>
	name=${name }
	<form action="<%=request.getContextPath()%>/test3/post_param.action" method = "post"><!--<span style="font-family: Arial, Helvetica, sans-serif;"><%=request.getContextPath()%>  获取projectName</span>-->
		id:<input type ="text" name="id"/> <br>
		姓名:<input type="text" name="name"/>
		<input type="submit" value = "发送"/>
	</form>
</body>
访问方式
localhost:8080/projectName/packageName/income.action  将会跳转到hello.jsp页面,此时id 和 name都为空,输入相应的值,点击发送,将跳转到post_param.action执行,并跳转回hello.jsp  此时id和name有值。


三、使用符合类型接收参数

在src中建立包 cn.xs.bean   并在此包里建立person类:

// ---------------------------------------------------------
// @author    sheng.xu
// @version   1.0.0
// @date	2014年6月3日
// ---------------------------------------------------------
package cn.xs.bean;

/**
 * @author sheng.xu
 *
 */
public class Person {
	private String name;
	private Integer id;
	
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}
	
}
在HelloWorldAction.java 中加入成员变量person 并建立相应的get和set方法;

struts.xml文件中加入

<action name="income" class="cn.xs.action.HelloWorldAction" method = "execute">
	<!-- 配置返回结果 -->
	<result name="success">/WEB-INF/page/hello.jsp</result>
	<result name="error">/error.jsp</result>
</action>
<action name="post_param" class="cn.xs.action.HelloWorldAction" method = "execute">
	<!-- 配置返回结果 -->
	<result name="success">/WEB-INF/page/hello.jsp</result>
	<result name="error">/error.jsp</result>
</action>
在hello.jsp代码如下:

<body>
	${message}    <br>xxxxxxx
	id=${person.id }<br>
	name=${person.name }
	<form action="<%=request.getContextPath()%>/test3/post_param.action" method = "post"><!--<span style="font-family: Arial, Helvetica, sans-serif;"><%=request.getContextPath()%>  获取projectName</span>-->
		id:<input type ="text" name="person.id"/> <br>
		姓名:<input type="text" name="person.name"/>
		<input type="submit" value = "发送"/>
	</form>
</body>

访问方式
localhost:8080/projectName/packageName/income.action  将会跳转到hello.jsp页面,此时id 和 name都为空,输入相应的值,点击发送,将跳转到post_param.action执行,并跳转回hello.jsp  此时id和name有值。

注:建议使用符合类型接收参数

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值