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中的条件?
答案 0 :(得分:2)
我可以看到这个问题的三个解决方案: