Search code examples
javajspstruts

How to Access a label value in Struts ActionForm on controller


I am using struts MVC. I want to access a label value in the controller by the struts ActionForm. In the controller I can access the value of text field values by the ActionForm, because that have 'name' field.But in label ,only 'id' is there.So please help me to access the label value to the controller by the ActionForm.

jsp

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

Controller

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>

Solution

  • You can add a hidden field with the label field and set the value to the hidden field also.You can set the hidden field name to that label name.

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