首先,Struts2是通过Action来接受前台数据的,而且Action必须继承ActionSupport类。
例如,在前台登录页面有username和password两个属性。代码如下:[/align]
login.jsp
<%@ 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>
那么在对应的Action中,如果想接受到用户输入的信息,就必须有和前台(login.jsp)同名的属性。也就是说,LoginAction中必须有username和password两个属性,并且添加他们的get()和set()方法。当然,LoginAction要实现ActionSupport,代码如下:
LoginAction.java
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"; } }
这样后台就可以得到前台的数据了。
二、后台-->前台的数据传递
方法一
Action.java
public String list() { List list=confirm.list(); ActionContext ct=ActionContext.getContext(); ct.put("box", list); return "list"; }
用ActionContext.getContext()方法得到PageContext对象。ct.put()方法是将list链表放到名为“box”的里面。box名是任意的。这样就可以在前台获取了。
<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>
上面用到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里根对象,进而选择用哪种方法。
方法二
result.jsp
<%@ 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>
LoginAction.java
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"; }