Spring整合Struts2和Hibernate+Maven(三)之请求的处理

来源:互联网 发布:黄帝大战蚩尤电影知乎 编辑:程序博客网 时间:2024/06/06 19:52

关于请求的处理,即涉及前面提到Struts2。
具体流程:页面发出请求->拦截action->处理action->具体到那个类的哪个方法处理。

页面发出请求:

        fm.action="/Login_register";        fm.submit();
        fm.action="/Login_login";        fm.submit();

这里的action参数为‘Login_register’,Login为前缀,register和login为方法名(之后会介绍为何register为方法名)。

拦截action:

<filter> <filter-name>struts2</filter-name> <filter-class>      org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter         </filter-class></filter>    <filter-mapping>        <filter-name>struts2</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>

处理action:
参看配置文件

<action name="/Login_*" class="cm.action.user.LoginAction" method="{1}"><result name="loginsuccess">/index.jsp</result><result name="registersuccess">/lv/login/registersuccess.jsp</result><result name="error">/lv/login/register.jsp</result></action>

action中的属性 name:这里用的是通配符,所有前缀为/Login_的action都会在class:LoginAction中处理,且method为后缀。即前面说的register和login。

具体类处理请求

package cm.action.user;import com.opensymphony.xwork2.ActionSupport;/** * Created by online on 17-4-14. */public class LoginAction extends ActionSupport {    private String username;    private String password;    public void setUsername(String username) {        this.username = username;    }    public void setPassword(String password) {        this.password = password;    }    public String login(){        String result="";        System.out.println(username+":"+password);        result="loginsuccess";        return result;    }    public String register(){        String result="";        System.out.println(username+":"+password);        result="registersuccess";        return result;    }}

在cm.action包下创建后缀为Action 的类,并继承ActionSupport,请求中的参数名称对应该类中的私有属性,并且需要为这些属性填写set方法(get方法可以不使用),login和register方法返回值类型为String,根据返回值确定结果请求哪个页面。

1 0
原创粉丝点击