Struts2的几种传参方式

本文介绍了在Struts2框架中使用不同方法传递参数至JSP页面并展示结果的方式,包括EL表达式、Struts自带标签、ActionContext.put方法、servlet API以及前端表单传递。

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

第一种:(EL表达式传参)
***UserAction.java***
package org.zttc.itat.action;
public class UserAction {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String addInput(){
        return "success";
    }

    public String add(){
        System.out.println("add");
        return "r_list";
    }

    public String list(){
        //此处进行数据设置JSP页面中可以通过EL表达式获取
        this.setUsername("张三");
        this.setPassword("123");
        return "success";
    }
}

***list.jsp***
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
<h1>User list</h1>
${username}--${password}
</body>
</html>
第二种:(Struts自带标签 注:在jsp中加入标签taglib)
***list.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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>User list</h1>
<s:property value="username"/>---<s:property value="password"/>
</body>
</html>
第三种:(通过ActionContext.getContext().put(String,String)方式)
***UserAction.java***
package org.zttc.itat.action;
import com.opensymphony.xwork2.ActionContext;
public class UserAction {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String addInput(){
        return "success";
    }

    public String add(){
        System.out.println("add");
        return "r_list";
    }

    public String list(){
        ActionContext.getContext().put("aaa", 123);
        ActionContext.getContext().put("bbb", 456);
        ActionContext.getContext().put("ccc", "678");
        return "success";
    }

}


***list.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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>User list</h1>
${aaa}---${bbb}---${ccc}
<!-- 使用s:property来访问actionContext中的数据都需要加#,在struts2.3之后,如果ActionContext中的数据
是String的类型就不用加#,但是在将来的开发中,只要是ActionContext中的数据一定加个#访问,所以下面也可以是value="#ccc" -->
<s:property value="#aaa"/>---<s:property value="#bbb"/>---<s:property value="ccc"/>
</body>
</html>
第四种:(通过servlet的API传值方式)
***UserAction.java***
package org.zttc.itat.action;
import org.apache.struts2.ServletActionContext;
public class UserAction {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String addInput(){
        return "success";
    }

    public String add(){
        System.out.println("add");
        return "r_list";
    }

    public String list(){
        ServletActionContext.getRequest().setAttribute("aaa", "123");
        return "success";
    }

}


***list.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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>User list</h1>
<s:property value="#request.aaa"/>
</body>
</html>
第五种(前端form表单传给后台)
在url中带参数
http://localhost:8090/struts2/User_addInput.action?username=abcd&password=123

后台action必须带有getter setter的方法就自动获取
***UserAction.java***
package org.zttc.itat.action;
import org.apache.struts2.ServletActionContext;
public class UserAction {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String addInput(){
        System.out.println(username+","+password);
        return "success";
    }

    public String add(){
        System.out.println("add");
        return "r_list";
    }

    public String list(){
        ServletActionContext.getRequest().setAttribute("aaa", "123");
        return "success";
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值