【问题标题】:How to get the value of variable from action class of Struts 2 to textfield of Jsp?如何从 Struts 2 的动作类中获取变量的值到 Jsp 的文本字段?
【发布时间】:2012-12-17 09:14:44
【问题描述】:

我的动作类是这样的

class My action class {

  public String getvariable() {
    return variable;
  }

  public void setvariable(String variable) {
    this.variable = variable;
  }

  public dosubmit(){
    variable=service.getvariable();
    return "success";
  }
}

通过使用struts的这个属性标签 我能够在 JSP 中获取变量的值。

但我想要 JSP 的文本字段中的值。 我该怎么做?

如何在下一个jsp文件的jsp变量中获取这个变量的值,这样我就可以操作这个值了。

【问题讨论】:

    标签: jsp struts2


    【解决方案1】:

    为变量创建 Getter 和 Setter,并在 JSP 中赋予相同的名称。 U 将获得变量的值。

    【讨论】:

      【解决方案2】:

      您可以使用<s:textfield>标签的value属性来预设值。

      <s:textfield name="some_name" value="%{variable}"/>
      

      【讨论】:

        【解决方案3】:

        在 Struts UI 标签中,name 属性用于在提交值时匹配 Setter 对象,而value 属性用于为对象预设任意值,该值可以(但不是必须)是您的变量价值。

        这个

        <s:textfield name="variable" value="%{variable}"/>
        

        表示你预置了变量的值,发布时会设置为新的变量值。

        请注意,您的操作在语法上是错误的; 对于 getter 和 setter(以及方法和变量名称等),您必须使用 CamelCase,如下所示:

        public class MyAction class {
        
          private String variable;
        
          public String getVariable() { 
            return variable;
          }
        
          public void setVariable(String variable) {
            this.variable = variable;
          }
        
          public doSubmit(){
            variable=service.getVariable();
            return "success";
          }
        }
        

        否则您将无法从 OGNL 获取值。

        【讨论】:

          猜你喜欢
          • 2015-04-03
          • 2012-05-17
          • 1970-01-01
          • 2016-07-13
          • 1970-01-01
          • 2014-12-14
          • 1970-01-01
          • 2012-06-04
          • 1970-01-01
          相关资源
          最近更新 更多