1
public class CustomerAction extends ActionSupport implements
        ModelDriven<CustomerForm> {

    @Autowired
    private CustomerService customerService;
    // private UserService userService;
    int userId = getUserId();

    CustomerForm customerform = new CustomerForm();

    public CustomerForm getModel() {
        // TODO Auto-generated method stub
        return customerform;
    }

    public String updateProfile() {
        System.out.println("Inside Update action");
        if (ServletActionContext.getRequest().getMethod() == "GET") {

            // User user = userService.getUser(userId);
            Customer user = customerService.getUser(userId);
            getModel().setId(user.getId());
            getModel().setName(user.getName());
            getModel().setEmail(user.getContact().getEmail());
            getModel().setAddress(user.getContact().getAddress());
            getModel().setGender(user.getGender());
            getModel().setIsMarried(user.getIsMarried());
            getModel().setCity(user.getContact().getCity());
            getModel().setPin(user.getContact().getPin());
            getModel().setMobile(user.getContact().getMobile());
            getModel().setOccupation(user.getOccupation());

            return "get";
        } else if (ServletActionContext.getRequest().getMethod() == "POST") {

            customerService.upadteProfile(userId, customerform.getEmail(),
                    customerform.getMobile(), customerform.getOccupation());

            return "post";

        }
        return "error";
    }

    private int getUserId() {
        HttpSession session = ServletActionContext.getRequest().getSession(
                false);
        LoginForm form = (LoginForm) session.getAttribute("user");
        return form.getUsername();
    }

    public String getAccountDetails()

    {
        System.out.println("Inside account details");
        System.out.println(userId);
        Customer user = customerService.getUser(userId);
        Set<Account> accounts = customerService.getAccountDetails(userId);
        getModel().setAccounts(accounts);
        getModel().setName(user.getName());
        return "success";
    }

    public String registerPayee() {
        if (ServletActionContext.getRequest().getMethod() == "GET") {
            return "get";
        } else if (ServletActionContext.getRequest().getMethod() == "POST") {
            String password=getModel().getPassword();
                customerService.registerPayee(getModel().getAccId(),userId,password);
                return "post";
            }


        return "error";
    }

}

For my CustomerAction class everytime i have to check in methods if it is a get call or post.

Struts.xml is like this:

<action name="registerpayee" class="com.techlabs.action.CustomerAction" 
      method="registerPayee">
    <result name="get">
        /pages/registerpayee.jsp
    </result>
    <result name="post">
        /pages/accountDetails.jsp
    </result>
</action>

I dont want to check for get-post conditions in my action class's method.Is there any alternative so that i can directly check conditions in struts.xml?

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
new coder
  • 73
  • 1
  • 1
  • 8

1 Answers1

2

I can see three solutions to this problem:

  1. Create a Custom Interceptor and do the work there (preferred);
  2. Create a Base Action extended by all the other actions. You will then return a BaseAction's method from the descendant Actions, that will return the appropriate result;
  3. Use the Struts2-REST-plugin.
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243