【问题标题】:passing struts2 perameter to different webpage将 struts2 参数传递给不同的网页
【发布时间】:2014-01-17 20:46:36
【问题描述】:

我有一个 struts2 动作类,当用户按下链接时执行

public class goToUserProfile {
    public String execute(){
        System.out.println("An action class has been called!");
        return "profile";
    }
}

这是标签

<s:a action="goToUserProfile">

为了显示配置文件的特定信息,我需要使用 struts 传递用户 ID

/profile.action?userNo=846258

事情是这样的。我真的不知道如何通过动作类传递值 假设我有一个数组中的所有用户 ID(目前)我如何将其中一个 ID 传递到个人资料页面?

【问题讨论】:

    标签: java web struts2 tags


    【解决方案1】:

    有几种方法可以做到这一点,在这个例子中,

    1)创建一个名为 userNo 的私有 Long 变量

    我选择 Long 是因为您的 userNo 看起来将是一个“大”整数

    2) 为 userNo 创建一个 getter 和 setter

    public class goToUserProfile {
        private Long userNo;
        public Long getUserNo(){
            return this.userNo
        }
        public void setUserNo(String rhs){
            this.userNo;
        }
        public String execute(){
            System.out.println("An action class has been called!");
            return "profile";
        }
    }
    

    当你链接到/profile.action?userNo=846258 setter 方法将在操作设置userNo 上调用。 当操作转发到 profile.jsp,? 或任何你称之为它的东西时,你可以使用类似

    的东西访问 userNo
    <s:property value="userNo"/>
    

    另外,如果你不想在你的动作类中创建一个字段,你可以像你一样调用你的动作/profile.action?userNo=846258 并在你的jsp中像这样访问它

    <s:property value="%{#parameters['userNo']}"/>
    

    这种方式访问​​请求参数本身。

    希望有帮助

    【讨论】:

      猜你喜欢
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      • 2016-03-23
      • 2016-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多