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";
    }

}

对于我的 CustomerAction 类,每次我必须检查方法是获取调用还是发布。

Struts.xml 是这样的:

<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>

我不想在我的操作类的方法中检查 get-post 条件。是否有任何替代方法可以直接检查 struts.xml 中的条件?

4

1 回答 1

2

我可以看到这个问题的三个解决方案:

  1. 创建一个自定义拦截器并在那里完成工作(首选);
  2. 创建一个由所有其他操作扩展的基本操作。然后,您将从后代 Actions 返回 BaseAction 的方法,该方法将返回适当的结果;
  3. 使用Struts2 -REST-plugin
于 2015-10-27T10:09:05.620 回答