1

I have a struts method and it was working fine before i added the validation, but once i added the validation method the method which i call is not called and it shows no exception also.

struts-config.xml

<action path="/RolesAction"  name="SecurEyesForm" type="com.secureyes.eswastha.struts.web.action.RolesAction" scope="request" parameter="method" validate="true" input="/WEB-INF/Masters/RolesMaster.jsp">
  <forward name="rolesHome" path="/WEB-INF/Masters/RolesMaster.jsp"></forward>        
</action>

action class :

package com.secureyes.eswastha.struts.web.action;

import com.secureyes.eswastha.struts.web.dao.RolesDAO;
import com.secureyes.eswastha.struts.web.viewmodel.RolesViewModel;
import com.secureyes.eswastha.struts.web.viewmodel.UserViewModel;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;

public class RolesAction extends SecureyesAction {

    private String forwardRequestTo = "authentication-failed";

    public ActionForward goToHome(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        DynaValidatorForm formObj = (DynaValidatorForm) form;
        int resourceid = 0;
        String pageToken = "";
        boolean isValidMenuRequest = false;
        UserViewModel userVM = new UserViewModel();

        if (request.getSession(false).getAttribute("userDetails") != null) {
            userVM = (UserViewModel) request.getSession(false).getAttribute("userDetails");
        }
        if (formObj.get("pagetoken") != null) {
            pageToken = formObj.get("pagetoken").toString();
        }

        if (formObj.get("resourceid") != null) {
            resourceid = Integer.parseInt(formObj.get("resourceid").toString());
        }

        boolean isValidPageToken = isValidPageToken(userVM.getPagetoken(), pageToken);
        if (isValidPageToken) {
            isValidMenuRequest = isValidMenuRequest(resourceid, userVM.getUserRoleid());
            if (isValidMenuRequest) {
                forwardRequestTo = "rolesHome";
            }
        }
        disableAutocompleteFeature(response);
        return mapping.findForward(forwardRequestTo);
    }

    public ActionForward saveRolesDetails(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        System.out.println("save roles");

        DynaValidatorForm formObj = (DynaValidatorForm) form;
        String rolename = "", description = "", functionType = "", roleid = "", saveOrupdateStatus = "", pageToken = "";
        int loggedinuserid = 0;
        UserViewModel userVM = new UserViewModel();
        RolesViewModel dvm = new RolesViewModel();
        RolesDAO rolesDAO = new RolesDAO();

        if (formObj.get("rolename") != null) {
            rolename = formObj.get("rolename").toString();
        }

        if (formObj.get("description") != null) {
            description = formObj.get("description").toString();
        }

        if (formObj.get("functionType") != null) {
            functionType = formObj.get("functionType").toString();
        }

        if (formObj.get("roleid") != null) {
            roleid = formObj.get("roleid").toString();
        }

        if (formObj.get("pagetoken") != null) {
            pageToken = formObj.get("pagetoken").toString();
        }

        if (request.getSession(false).getAttribute("userDetails") != null) {
            userVM = (UserViewModel) request.getSession(false).getAttribute("userDetails");
        }

        loggedinuserid = userVM.getUserid();

        boolean isValidPageToken = isValidPageToken(userVM.getPagetoken(), pageToken);
        if (isValidPageToken) {
            if (functionType.equals("add")) {
                dvm.setRolename(rolename);
                dvm.setDescription(description);
                dvm.setUserid(loggedinuserid);
                saveOrupdateStatus = rolesDAO.saveRolesDetails(dvm, functionType);
            } else if (functionType.equals("edit")) {
                dvm.setRoleid(Integer.parseInt(roleid));
                dvm.setRolename(rolename);
                dvm.setDescription(description);
                dvm.setUserid(loggedinuserid);
                saveOrupdateStatus = rolesDAO.saveRolesDetails(dvm, functionType);
            }
            request.setAttribute("message", saveOrupdateStatus);
            forwardRequestTo = "rolesHome";
        }
        return mapping.findForward(forwardRequestTo);
    }

    public ActionForward deleteRoles(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DynaValidatorForm formObj = (DynaValidatorForm) form;
        String saveOrupdateStatus = "", pageToken = "", roleid = "";
        RolesDAO rolesDAO = new RolesDAO();
        UserViewModel userVM = new UserViewModel();

        if (formObj.get("roleid") != null) {
            roleid = formObj.get("roleid").toString();
        }

        if (formObj.get("pagetoken") != null) {
            pageToken = formObj.get("pagetoken").toString();
        }

        if (request.getSession(false).getAttribute("userDetails") != null) {
            userVM = (UserViewModel) request.getSession(false).getAttribute("userDetails");
        }

        boolean isValidPageToken = isValidPageToken(userVM.getPagetoken(), pageToken);
        if (isValidPageToken) {
            saveOrupdateStatus = rolesDAO.deleteRoles(Integer.parseInt(roleid));
            request.setAttribute("message", saveOrupdateStatus);
            forwardRequestTo = "rolesHome";
        }
        return mapping.findForward(forwardRequestTo);
    }

    public List getAutocompleteRolesData(String rolesName) {
        List rolesNameList = new ArrayList<String>();
        RolesDAO rolesDAO = new RolesDAO();
        try {
            rolesNameList = rolesDAO.getAutocompleteRolesData(rolesName);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return rolesNameList;
    }

    public List getRolesDescriptionAutocompleteData(String rolesDescription) {
        List rolesNameList = new ArrayList<String>();
        RolesDAO rolesDAO = new RolesDAO();
        try {
            rolesNameList = rolesDAO.getRolesDescriptionAutocompleteData(rolesDescription);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return rolesNameList;
    }

    private String constructRolesSearchQuery(String rolename, String description) {
        StringBuilder rolesSearchQuery = new StringBuilder();
        rolesSearchQuery.append("select roles.rolename,roles.description,roles.roleid from m_roles as roles ");
        if (!rolename.equals("")) {
            rolesSearchQuery.append(" where roles.rolename='").append(rolename).append("' ");
        }
        if (!description.equals("")) {
            if (!rolename.equals("")) {
                rolesSearchQuery.append(" and roles.description='").append(description).append("'");
            } else {
                rolesSearchQuery.append(" where roles.description='").append(description).append("'");
            }
        }
        return rolesSearchQuery.toString();
    }

    public Map getRolesResultHTML(String rolename, String description,HttpServletRequest request) {
        Map roleHTMLMap = new LinkedHashMap();
        String roleQuery = "";
        String path=request.getContextPath();
        roleQuery = constructRolesSearchQuery(rolename, description);
        RolesDAO rolesDAO = new RolesDAO();
        roleHTMLMap = rolesDAO.getRolesResultHTML(roleQuery,path);
        return roleHTMLMap;
    }

    public boolean isRolesExist(String rolename) {
        boolean isRolename = false;
        RolesDAO rolesDAO = new RolesDAO();
        isRolename = rolesDAO.isRolesExist(rolename);
        return isRolename;
    }

    public Map getRoles() {
        Map map = new LinkedHashMap();
        RolesDAO dao = new RolesDAO();
        map = dao.getRoles();
        return map;
    }
}

and this is how i call the method :

function dataSave() {
                alert("data Save");
                getBlanket('continueDIV');
                document.forms[0].action="RolesAction.htm";
                document.forms[0].method.value="saveRolesDetails";
                document.forms[0].submit();   
            }
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Java Questions
  • 7,813
  • 41
  • 118
  • 176
  • ... Not sure what you expect. We don't know what you added. How about you add some logging and see where it's failing? – Dave Newton Nov 18 '12 at 14:00
  • Thanks for the response, I tried to print some message on the console `saveRolesDetails` but the control does not fall there. – Java Questions Nov 18 '12 at 14:02
  • 1
    Did you check your JavaScript console for errors? Is your logging at DEBUG so you can see any framework errors? What do you mean by "added validation"? And, SQL injection. – Dave Newton Nov 18 '12 at 14:09
  • after adding server side validation = added validations also SQL injection, how to find that – Java Questions Nov 18 '12 at 14:10
  • Then something's wrong with your validation, or it's failing. Re: SQL injection, you're not using prepared statements--you're just building strings without escaping. [Little Bobby Tables](http://xkcd.com/327/). – Dave Newton Nov 18 '12 at 14:15
  • SQL injection thanks for the info, I will fix this but how to find the other one? – Java Questions Nov 18 '12 at 14:17
  • Fix your validation? See if it goes back to working if you take it back out? Who knows--like I said, we don't know what you added, what changed, anything. – Dave Newton Nov 18 '12 at 14:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19709/discussion-between-anto-and-dave-newton) – Java Questions Nov 18 '12 at 14:23
  • If what you added is `validate="true"` in your config, and the form you submit is invalid, then STruts will directly forward to the `input` JSP. That's how validation works. – JB Nizet Nov 18 '12 at 17:28
  • exactly same, the control goes to input page specified with out submitting the form, How to fix this – Java Questions Nov 18 '12 at 17:43
  • By submitting valid data, or by fixing the form's validate method if it returns validation errors when it should not. – JB Nizet Nov 18 '12 at 20:41

0 Answers0