一、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有值。
注:建议使用符合类型接收参数