Java 如何区分get和post call。。? 公共类CustomerAction扩展了ActionSupport实现 模型驱动{ @自动连线 私人客户服务; //私人用户服务; int userId=getUserId(); CustomPerform CustomPerform=新CustomPerform(); 公共CustomerPerform getModel(){ //TODO自动生成的方法存根 返回客户执行; } 公共字符串updateProfile(){ System.out.println(“内部更新操作”); if(ServletActionContext.getRequest().getMethod()=“GET”){ //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()); 返回“get”; }else if(ServletActionContext.getRequest().getMethod()=“POST”){ customerService.upadteProfile(用户ID,CustomerPerform.getEmail(), customerform.getMobile(),customerform.getocculation(); 返回“post”; } 返回“错误”; } 私有int getUserId(){ HttpSession session=ServletActionContext.getRequest().getSession( 假); LoginForm form=(LoginForm)session.getAttribute(“用户”); 返回form.getUsername(); } 公共字符串getAccountDetails() { System.out.println(“内部账户详细信息”); System.out.println(userId); Customer user=customerService.getUser(userId); Set accounts=customerService.getAccountDetails(userId); getModel().setAccounts(accounts); getModel().setName(user.getName()); 返回“成功”; } 公共字符串注册表页(){ if(ServletActionContext.getRequest().getMethod()=“GET”){ 返回“get”; }else if(ServletActionContext.getRequest().getMethod()=“POST”){ 字符串密码=getModel().getPassword(); customerService.RegisterPaye(getModel().getAccId(),用户ID,密码); 返回“post”; } 返回“错误”; } }

Java 如何区分get和post call。。? 公共类CustomerAction扩展了ActionSupport实现 模型驱动{ @自动连线 私人客户服务; //私人用户服务; int userId=getUserId(); CustomPerform CustomPerform=新CustomPerform(); 公共CustomerPerform getModel(){ //TODO自动生成的方法存根 返回客户执行; } 公共字符串updateProfile(){ System.out.println(“内部更新操作”); if(ServletActionContext.getRequest().getMethod()=“GET”){ //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()); 返回“get”; }else if(ServletActionContext.getRequest().getMethod()=“POST”){ customerService.upadteProfile(用户ID,CustomerPerform.getEmail(), customerform.getMobile(),customerform.getocculation(); 返回“post”; } 返回“错误”; } 私有int getUserId(){ HttpSession session=ServletActionContext.getRequest().getSession( 假); LoginForm form=(LoginForm)session.getAttribute(“用户”); 返回form.getUsername(); } 公共字符串getAccountDetails() { System.out.println(“内部账户详细信息”); System.out.println(userId); Customer user=customerService.getUser(userId); Set accounts=customerService.getAccountDetails(userId); getModel().setAccounts(accounts); getModel().setName(user.getName()); 返回“成功”; } 公共字符串注册表页(){ if(ServletActionContext.getRequest().getMethod()=“GET”){ 返回“get”; }else if(ServletActionContext.getRequest().getMethod()=“POST”){ 字符串密码=getModel().getPassword(); customerService.RegisterPaye(getModel().getAccId(),用户ID,密码); 返回“post”; } 返回“错误”; } },java,rest,http,struts2,struts-action,Java,Rest,Http,Struts2,Struts Action,对于我的CustomerAction类,每次我都必须签入方法,如果它是get调用或post Struts.xml如下所示: /pages/RegisterPaye.jsp /pages/accountDetails.jsp 我不想在action类的方法中检查get-post条件。是否有其他方法可以直接检查struts.xml中的条件?我可以看到解决此问题的三种方法: 创建一个自定义拦截器并在那里执行工作(首选) 创建一个由所有其他动作扩展的基本动作。然后,您将从子代操作返回BaseActio

对于我的CustomerAction类,每次我都必须签入方法,如果它是get调用或post

Struts.xml如下所示:


/pages/RegisterPaye.jsp
/pages/accountDetails.jsp

我不想在action类的方法中检查get-post条件。是否有其他方法可以直接检查struts.xml中的条件?

我可以看到解决此问题的三种方法:

  • 创建一个自定义拦截器并在那里执行工作(首选)
  • 创建一个由所有其他动作扩展的基本动作。然后,您将从子代操作返回BaseAction的方法,该方法将返回适当的结果
  • 使用
  • 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";
        }
    
    }