3. Refactor web layer
(1) LoginAction
public class LoginAction extends ActionSupport {
public String execute() {
if (userService.isUserValid(userDto.getUserid(), userDto.getPassword())) {
return SUCCESS;
}
setLoginError("login failure, userid or password incorrect!");
return INPUT;
}
private UserDto userDto;
private String loginError;
public UserDto getUserDto() {
return userDto;
}
public void setUserDto(UserDto userDto) {
this.userDto = userDto;
}
public String getLoginError() {
return loginError;
}
public void setLoginError(String loginError) {
this.loginError = loginError;
}
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
}
l Struts2 action extends com.opensymphony.xwork2.ActionSupport, overrides execute() method.
l The execute() method returns a control string(consist with name attribute of result element) that indicates which of its results should render the result page.
l Struts2 no longer use ActionForm any more. Struts2 action is not only an encapsulation of the calls to business logic but a locus of data transfer.
l Struts2 action use JavaBeans properties to hold the application domain data. The framework will set incoming data onto these properties when preparing the action for execution.
l Struts2 introduces ValueStack and OGNL. The mechanics of the ValueStack are such that all properties of the action will then be exposed as top-level properties of the ValueStack itself and, thus, accessible via OGNL in result pages.(see next section)
(2) login.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>User Login</title>
</head>
<body>
<h4>
User Login
</h4>
<s:form action="Login">
<s:textfield name="userDto.userid" label="userid" />
<s:textfield name="userDto.password" label="password" />
<s:submit />
</s:form>
<s:if test="loginError != null">
<s:property value="loginError" />
</s:if>
</body>
</html>
l Struts2 provides a new tag library(please refer to Struts2 tag reference),allow you to access domain data in the action(as top-level properties of ValueStack) via OGNL.
l Because action LoginInput and Login are in the same namespace, the namespace attribute of tag <s:form> is omitted.
You can download Struts2Spring Project from here .
Note: the jar files in the WEB-INF/lib directory are not involved.