Struts2 jsp页面和Action之间的传值方式

来源:互联网 发布:淘宝店关注链接 编辑:程序博客网 时间:2024/04/23 22:34

Action向jsp传值

首先要在Action中声明所有要传向jsp页面的值的属性
然后生成get、set方法

UserAction.java

package cn.qdsoft.actions;import java.sql.Date;import java.util.List;import javax.annotation.Resource;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Namespace;import cn.qdsoft.BaseAction;import cn.qdsoft.dao.UserDAO;import cn.qdsoft.model.User;import cn.qdsoft.service.UserService;@Namespace("/user")public class UserAction extends BaseAction {    // 必须声明为接口类型;important!!!    @Resource    private UserDAO userDAO;    private List<User> userList;    private User user;    private Long id;    private String login;    private String name;    private String passwd;    private int type;     private int status;    private Date lastLogin;    private Date gmtCreate;    private Date gmtModified;    private int createById;    private int lastModifiedById;    // 也在一个package中,package的名字不知道    // 父package 名字叫one    @Action("list")    public String list() {        userList = userDAO.findAll();        return SUCCESS;    }    @Action("delete")    public String delete() {        System.out.println("user delete............");        Long id = getId();        System.out.println(id);        return LIST;    }    public List<User> getUserList() {        return userList;    }    public void setUserList(List<User> userList) {        this.userList = userList;    }    public User getUser() {        return user;    }    public void setUser(User user) {        this.user = user;    }    public Long getId() {        return id;    }    public void setId(Long id) {        this.id = id;    }    public String getLogin() {        return login;    }    public void setLogin(String login) {        this.login = login;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPasswd() {        return passwd;    }    public void setPasswd(String passwd) {        this.passwd = passwd;    }    public int getType() {        return type;    }    public void setType(int type) {        this.type = type;    }    public int getStatus() {        return status;    }    public void setStatus(int status) {        this.status = status;    }    public Date getLastLogin() {        return lastLogin;    }    public void setLastLogin(Date lastLogin) {        this.lastLogin = lastLogin;    }    public Date getGmtCreate() {        return gmtCreate;    }    public void setGmtCreate(Date gmtCreate) {        this.gmtCreate = gmtCreate;    }    public Date getGmtModified() {        return gmtModified;    }    public void setGmtModified(Date gmtModified) {        this.gmtModified = gmtModified;    }    public int getCreateById() {        return createById;    }    public void setCreateById(int createById) {        this.createById = createById;    }    public int getLastModifiedById() {        return lastModifiedById;    }    public void setLastModifiedById(int lastModifiedById) {        this.lastModifiedById = lastModifiedById;    }}

如果在User.java中已有了一下代码

private Long id;    private String login;    private String name;    private String passwd;    private int type;     private int status;    private Date lastLogin;    private Date gmtCreate;    private Date gmtModified;    private int createById;    private int lastModifiedById;

以及get、set方法就不需要在action中写了,只需要定义private User user;以及user的get、set

list.jsp

<%@ page 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><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>  <table class='table  table-bordered table-hover table-striped'>    <thead>        <tr>            <th>序号</th>            <th>登录名</th>            <th>真实姓名邮件</th>            <th>密码</th>            <th>类型</th>            <th>状态</th>            <th>最后登录时间</th>            <th>创建时间</th>            <th>最后修改时间</th>            <th>创建人ID</th>            <th>最后更新人ID</th>            <th></th>            <th></th>        </tr>    </thead>    <tbody>        <!-- getUserList() -->        <s:iterator value="userList" status="s">            <tr>                <td><s:property value="#s.index+1"/></td>                <!-- getName() -->                <td><s:property value="login" /></td>                <td><s:property value="name" /></td>                <td><s:property value="passwd" /></td>                <td><s:property value="type" /></td>                <td><s:property value="status" /></td>                <td><s:property value="lastLogin" /></td>                <td><s:property value="gmtCreate" /></td>                <td><s:property value="gmtModified" /></td>                <td><s:property value="createById" /></td>                <td><s:property value="lastModifiedById" /></td>                <!--  /user/edit.action?id=2&name=tom -->                <td><s:a cssClass="ajax-link"   action="edit">                 <s:param name="id" value="id"/>                修改</s:a>                </td>                <td><s:a cssClass="ajax-link"   action="delete">                 <s:param name="id" value="id"/>                删除</s:a>                </td>            </tr>        </s:iterator>    </tbody></table></body></html>

value=”XXX” 是Action中的getXXX()方法
比如<td><s:property value="login" /></td>
就是getLogin()

jsp向 Action传值

这里写图片描述

统一name和value

这里写图片描述

struts2 <s:textfield> 标签与<s:property>标签value值设置为action属性值或者对象的属性值2.<s:textfield> 标签不能直接引用必须通过ognl表达式获取<s:textfield id="login" name="user.login" cssClass="form-control" value="%{account}" /><s:textfieldname="personAge" label="年龄" value="%{person.personAge}"/>
0 0
原创粉丝点击