Update Samples Project to Struts2 part3 (over)

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值