您的位置:首页 > 编程语言 > Java开发

Struts前后台传值

2015-10-31 21:28 423 查看
一.
前台向后台传值
    Struts利用action接受数据

Action extends ActionSupport
前台提交的post数据中,Action类中必须有同名的变量成员来接受,并且此成员变量必须有set方法。
页面中的代码:
<a href="${pageContext.request.contextPath}/adminOrder_findAll.action?page=1">首页</a>
后台action接受page变量
//页数
private Integer page;
public void setPage(Integer page) {
this.page = page;
}

Action extends  ActionSupport  implements  ModelDriven<实体>
模型驱动,是struts接受数据的一种最常用的方式,是struts面向对象发展的一种产物,其实第一种接受前台数据的方式,也是有这中方式发展而来。
页面中的代码
<form method="post"action="${pageContext.request.contextPath }/adminUser_login.action"target="_parent" name='theForm' onsubmit="returnvalidate()">
<tablecellspacing="0" cellpadding="0" style="margin-top:100px" align="center">
<tr>
<td><img src="images/login.png" width="178"height="256" border="0" alt="SHOP"/></td>
<tdstyle="padding-left: 50px">
<table>
<tr>
<td>管理员姓名:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>管理员密码:</td>
<td><input type="password" name="password"/></td>
</tr>

<tr>
<td> </td><td><inputtype="submit" value="进入管理中心"class="button"/></td>
</tr>
</table>
</td>
</tr>
</table>
<inputtype="hidden" name="act" value="signin" />
</form>
action中的代码
public class AdminUserAction extends ActionSupportimplements
ModelDriven<AdminUser> {
// 模型驱动使用的对象
private AdminUser adminUser = new AdminUser();

public AdminUser getModel() {
return adminUser;
}
}
我们可以通过断点调试,发现,由前台post方式传来的值,都已经给相应的后台实体的变量成员赋值。

 
 
二.
后台向前台传
保存到值栈中
action代码
ActionContext.getContext().getValueStack().set("cList",cList);
前台接受数据
<s:iterator var="c"value="cList">

<option value="<s:propertyvalue="#c.cid"/>"><s:property
value="#c.cname" /></option>
</s:iterator>

保存到session中
action中代码
ServletActionContext.getRequest().getSession().setAttribute("existUser",existUser);
前台接受数据
<s:property value="#session.organizaitonInfo.OrganizationNumber"/><span style="font-family: 幼圆; font-size: 12pt; background-color: rgb(255, 255, 255);"> </span>

上边是从网上商城项目中学到的一点东西,拿来分享。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: