Struts2接受请求参数

[size=large][color=red]1.采用基本类型接收请求参数(get/post)
在Action类中定义与请求参数同名的属性,struts2便能自动接收请求参数并赋予给同名属性。[/color]
创建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 'add.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">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>

<body>
<form action="<%=request.getContextPath() %>/renwen/test/helloworldother" method="post">
id:<input type="text" name="person.id">
name:<input type="text" name="person.name">
<input type="submit" value="提交">
</form>
</body>
</html>
action代码:
package com.renwen.action;
public class HelloWorldAction {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) [color=red]{//struts2通过反射技术调用与请求参数同名的属性的setter方法来获取请求参数值[/color] this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() throws Exception{
return "success";

}
public String other() throws Exception{
return "success";
}
}
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>
<!-- <include file="employee.xml"></include>
<include file="department.xml"></include>
-->
<constant name="struts.serve.static.browserCache" value="false"/>

<constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
<package name="renwen" namespace="/renwen/test" extends="struts-default">
<action name="helloworld*" class="com.renwen.action.HelloWorldAction"
method="{1}">
<result name="success">/WEB-INF/page/message.jsp</result>
</action>
</package>
</struts>
message.jsp页面如下:
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>第一个struts2应用</title>
</head>
<body>
id=${id} <br>
name=${name}
</body>
</html>
[color=red]2.采用复合类型接收请求参数[/color]
创建实体bean
代码如下
package com.renwen.bean;

public class Person {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int id;
private String name;
}
jsp,xml页面同上
action代码如下
action代码:
package com.renwen.action;
public class HelloWorldAction {
private Person person;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public String execute() throws Exception{
return "success";

}
public String other() throws Exception{
return "success";
}
}
[color=red]Struts2首先通过反射技术调用Product的默认构造器创建product对象,然后再通过反射技术调用product中与请求参数同名的属性的setter方法来获取请求参数值。[/color][/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值