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

本文介绍了Struts2框架下Action如何向jsp页面传递数据,以及jsp如何回传值到Action。在Action中声明并设置属性,如在UserAction中创建User对象的get、set方法。在jsp页面中,通过value属性调用Action的getter方法,如`value="${user.login}"`。同时,讲解了jsp向Action传值时的name和value的使用规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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}"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值