0

I have a form in JSP having two fields, and in action class I have an instance variable for each field, but those attributes are null when action class is executing.

I have used validate() method that is not even executing.

JSP

<s:form action="addAuthority">
    <table>
        <caption> <b><big>Add New Authority</big></b>

        </caption>
        <s:if test="hasActionErrors()">
            <tr>
                <td><s:actionerror />
                </td>
            </tr>
        </s:if>
        <s:if test="hasActionMessages()">
            <tr>
                <td><s:actionmessage />
                </td>
            </tr>
        </s:if>
        <tr>
            <td></td>
            <td>
                <s:textfield name="role" label="Authority Name"></s:textfield </td>
                <td></td>
                <td>
                    <s:select name="dependentAuthority" list="#request.authorityList" label="Dependent Authority" listKey="roleId" listValue="role"></s:select>
                </td>
                <td>
                    <s:submit value="Add"></s:submit>
                </td>
        </tr>
    </table>
</s:form>

Action

public class AddAuthorityAction extends ActionSupport {
private String dependentAuthority;
private String role;
private Map<String, Object>  session;
private HttpServletRequest request;
public String execute() throws Exception{
    HttpServletRequest request = ServletActionContext.getRequest();
    //System.out.print(role + "  " + dependentAuthority+"  ");

    role = request.getParameter("role");
    dependentAuthority = request.getParameter("dependentAuthority");
    //System.out.print(role+"  "+ dependentAuthority);

    //insert the data
    int count = new DBInsert().addRoleDependency(role, Integer.parseInt(dependentAuthority));
    if(count==0){
        addActionError("There is some error while inserting. Please try again");
    }else{
        addActionMessage("Information successfully inserted");
    }
    return SUCCESS;
}
@SuppressWarnings("unchecked")
public String moveAddAuthority() {
    Map request = (Map) ActionContext.getContext().get("request");

    List<Role> authorityList = new DBSelect().getAuthorityId();
    request.put("authorityList", authorityList);

    List<Role> roleWithDependency = new DBSelect().getRoleWithDependence();
    request.put("roleWithDependency", roleWithDependency);
    return SUCCESS;
}

public void validate() {
    if (role == null || role.trim().equals("")) {
        addFieldError("role", "The name is required");
    }
}   
public String getDependentAuthority() {
    return dependentAuthority;
}
public void setDependentAuthority(String dependentAuthority) {
    this.dependentAuthority = dependentAuthority;
}
public String getRole() {
    return role;
}
public void setRole(String role) {
    this.role = role;
}}
  1. when I am using HttpServletRequest request = ServletActionContext.getRequest(); I can get the value;
  2. but through implementing ServletRequestAware request become null;
  3. without using both instance variable is null;
  4. I could not get ActionMessage in JSP page.

struts.xml

<action name="addAuthority" class="nodue.action.AddAuthorityAction" 
      method="execute" >
        <interceptor-ref name="authonticateInterceptor"></interceptor-ref>
        <result name="success" type="redirect">moveAddAuthority</result>
    </action>
    <action name="moveAddAuthority" class="nodue.action.AddAuthorityAction" 
         method="moveAddAuthority">
        <interceptor-ref name="authonticateInterceptor"></interceptor-ref>
        <result name="success">/authority.jsp</result>
    </action>

I have made some modification on datatype of dependentAuthority previously it was Integer, and also added in JSP page the <s:if> tag.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Rakesh Ray
  • 62
  • 1
  • 9
  • I hope the getters and setters are public and have proper naming. Can you please also post the code for the same – wib May 29 '15 at 04:16
  • I have added the full Action class and also given related modified JSP tags. added another problem the "could not get ActionMessage in JSP page" – Rakesh Ray May 29 '15 at 05:34
  • 1
    Don't use request to get variables. Probably issue is in your interceptor stack, show it. – Aleksandr M May 29 '15 at 08:25

1 Answers1

0
  1. You are probably using a single Interceptor, while you should use an entire stack with your interceptor in it.

  2. You are returning the redirect result that should be used to reach external URLs or non-action URLs. To redirect to an action you should use redirectAction result.

  3. No matter if redirect or redirectAction, when you redirect you lose the request parameters, and this is why you don't see action errors and messages. There are different solutions to this.

  4. You use the validate() method, that returns INPUT result when things go south. But you haven't defined any INPUT result for your actions; be sure to understand how the INPUT result works, along with ValidationInterceptor and ParameterInterceptor.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243