struts2 的action 怎么向页面传值?

struts2 的action 怎么向页面传值?,第1张

action向jsp传值的方法有三种:

1.用request.setAttribute()方法,不过只适用于请求转发,不可用于重定向

2.用session.setAttribute()方法

3.用struts2标签,在Java类中定义这个值对象,然后赋值,并且给出setter和getter方法,在jsp页面中,使用

<s:property value=""/>来获取

1、浏览器往Action传递参数:

在Action中设置属性,并为属性设置get/set方法。传递参数时按照属性的名字赋值即可。如xxx?name=aa

取值用request.getPrameters("属性名“)

public class UserAction {

private String name

private User user

public String userAdd() {

System.out.print(user.getName())

System.out.print(name)

return "success"

}

public String getName() {

return name

}

public void setName(String name) {

this.name = name

}

public User getUser() {

return user

}

public void setUser(User user) {

this.user = user

}

}

注:struts2不会使用单例模式,因此每次的请求都是new 一个新对象。

2、Action 往浏览器界面传递参数:

方式有三种:

第一种:直接给Action 属性(有get/set方法)赋值 ,JSP中用

<s:property value="OGNL表达式"/>取值,注意返回结果类型为forward

例如:

<s:property value="name"/>

<s:property value="user.name"/>

第二种:通过ActionContext传值,在Action中所调用的方法中加入:

ActionContext.getContext().put("key", "value")

JSP中用<s:property value="#key"/>取值

第三种:通过request、session 传值。Action方法中通过取得HttpervletRequest 、HttpSession 和 Map对象设置值,

例如:

ServletActionContext.getRequest().setAttribute("arg0", "value")

ServletActionContext.getRequest().getSession().setAttribute("arg0", "value")

ActionContext.getContext().getSession().put("key", "value")

把你Action中的setListcid(String listcid)改成setListssequence(String listcid)

public void setListssequence(String listcid){

this.listcid = listcid

}

Action的传值是通过Set方法传进去的,Action的成员可以和Form表单的名称不一样


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/bake/11944960.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-19
下一篇2023-05-19

发表评论

登录后才能评论

评论列表(0条)

    保存