Struts2前台后台的数据传递 (转)

[size=large]一、前台-->后台的数据传递 [/size]


[color=red] 首先,Struts2是通过Action来接受前台数据的,而且Action必须继承ActionSupport类。
例如,在前台登录页面有username和password两个属性。代码如下:[/align][/color]


[size=medium][color=darkred]
login.jsp[/color][/size]

<%@ page language="java" contentType="text/html; charset=GB2312" pageEncoding="GB2312" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<s:form action="LoginAction" method="post" namespace="/" >
<s:label value="登录"></s:label>
<s:textfield name="userName" label="请输入用户名"></s:textfield>
<s:password name="password" label="请输入密码"></s:password>
<s:submit value="登录" align="left" method="login"></s:submit>
<s:submit value="注册" align="left" method="regist"></s:submit>
</s:form>
</body>
</html>


[color=red]那么在对应的Action中,如果想接受到用户输入的信息,就必须有和前台(login.jsp)同名的属性。也就是说,LoginAction中必须有username和password两个属性,并且添加他们的get()和set()方法。当然,LoginAction要实现ActionSupport,代码如下:[/color]
[size=medium][color=darkred]
LoginAction.java[/color][/size]
public class LoginAction extends ActionSupport{

// 用户名
private String username;

// 用户密码
private String password;


public String execute() {
confirm.add(username, password);
System.out.println(username);
System.out.println(password);
return "success";

}

}


[color=red]这样后台就可以得到前台的数据了。[/color]


[size=large]二、后台-->前台的数据传递 [/size]
[size=medium][color=brown]方法一[/color][/size]
[size=medium][color=darkred]
Action.java[/color][/size]

public String list() {

List list=confirm.list();
ActionContext ct=ActionContext.getContext();
ct.put("box", list);
return "list";
}

[color=red]
用ActionContext.getContext()方法得到PageContext对象。ct.put()方法是将list链表放到名为“box”的里面。box名是任意的。这样就可以在前台获取了。[/color]

<table>
<s:iterator value="box" id="li"></s:iterator>
<tr>
<td><s:property value="#li.username"/></td>
<td><s:property value="#li.password"/></td>
<td><a href="delete.action?id=<s:property value="#li.id"/>"/></td>
</tr>
</table>

[color=red]上面用到Struts2标签,这里不做解释,请查API。iterator用来遍历集合。value值就是后台用put()方法放入的名称,上题是box。property是输出单一属性,value为输出内容。这里重点说一下OGNL表达式。

OGNL为表达式,用来显示对象属性。用OGNL显示属性有两种:

1,当访问OGNL的Stack context里根对象的属性时,可以省略对象名。

如:若foo为context的根对象,假设foo有blah属性,前台获取该属性时,只需写:blah;//调用了foo的getBlah()方法返回属性值。

2,当访问OGNL的Stack context里非根对象的属性时,要用#对象名.属性访问。

如:#person.name

下面的问题就是如何确定哪些是OGNL的Stack context里根对象,进而选择用哪种方法。[/color]


[size=medium][color=brown]方法二[/color][/size]


[size=medium][color=darkred]result.jsp[/color][/size]
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>提交结果</title>
</head>
<body>
<h1>${result}</h1>
</body>
</html>


[size=medium][color=darkred]LoginAction.java[/color][/size]

package com.struts.action;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
import com.struts.service.LoginService;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport implements ServletRequestAware {

// 用户名
private String userName;

// 用户密码
private String password;

// Service
private LoginService service = new LoginService();

// 获得HttpServletRequest对象
private javax.servlet.http.HttpServletRequest request;

public void setServletRequest(HttpServletRequest request) {
this.request = request;
}

/**
* 登录
* @return
*/
public String login() throws Exception {

// 登录用户check
boolean flg = service.userLogin(userName, password);

if (flg) {

request.setAttribute("result", "成功保存[" + "登录成功" + "]");

return "login";
}

request.setAttribute("result", "成功保存[" + "登录失败" + "]");

return "login";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值