如何区分获取和发布电话..?

时间:2015-10-27 08:45:03

标签: java rest http struts2 struts-action

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中的条件?

1 个答案:

答案 0 :(得分:2)

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

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