【问题标题】:How to Access a label value in Struts ActionForm on controller如何在控制器上的 Struts ActionForm 中访问标签值
【发布时间】:2017-01-21 11:12:04
【问题描述】:

我正在使用 struts MVC。我想通过 struts ActionForm 访问控制器中的标签值。在控制器中,我可以通过 ActionForm 访问文本字段值的值,因为它具有“名称”字段。但在标签中,只有“id”。所以请帮助我通过 ActionForm 访问控制器的标签值.

jsp

<html:form action="action.do">
<label  id="labelvalue">label_Value</label>
</html:form>

控制器

public ActionForward defaultMethod(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
try{
    StrutsActionForm actform;       
    actform= (StrutsActionForm) form;
    String labelvalue=actform.getLabelValue();//now it shows null.I want to get the value from the label field
    return "success";
  }catch(Exception e){
   return null;
}
}

StrutsActionForm

publi class StrutsActionForm  extends ActionForm{
private String labelvalue;
public String getLabelValue() {
 return labelvalue;
}
public void setLabelValue(String labelvalue) {
this.labelvalue= labelvalue;
}
}

struts-config.xml

<form-beans>
<form-bean name="strutsActionForm" type="com.StrutsActionForm"></form-bean>
</form-beans>
<action-mappings>
<action path="/action" name="strutsActionForm" input="/index.jsp" 
        type="com.Controller">
        <forward name="success" path="/successWindow" />
</action> 
</action-mappings>

【问题讨论】:

    标签: java jsp struts


    【解决方案1】:

    您可以使用标签字段添加隐藏字段并将值设置为隐藏字段。您可以将隐藏字段名称设置为该标签名称。

    <html:form action="action.do">
    <label  id="labelvalue">label_Value</label>
    <input type="hidden" name="labelvalue" value="label_Value"/> 
    </html:form>
    

    【讨论】:

      【解决方案2】:

      您可以使用此代码访问标签标签中的文本

      <html>
          <head>
              <script>
                  function getLabelValue() {
      	        alert(document.getElementById("labelvalue").innerText);
      	    }
      	</script>
          </head>
          <body>
      	<html:form>
      	    <label id="labelvalue">FOOOOOOO</label>
      	    <input type="button" onclick="getLabelValue()"/>
              </html:form>
          </body>
      </html>

      现在向 JSP 添加一个隐藏属性(Form 类中的一个属性)。提交时调用这个 JS 函数

      function setLabelValueToFormAttribute()
      {
          document.getElementById("hiddenProperty").value = document.getElementById("labelvalue").innerText;
      }
      

      【讨论】:

      • 还有其他方法吗?因为我的jsp页面有50条数据
      • 在从服务器获取响应时,标签内的文本是否不断变化,还是每次响应都保持不变。
      • 你可以使用属性文件来显示标签。使用java类你可以读取该属性。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多