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

关于请求的处理,即涉及前面提到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,根据返回值确定结果请求哪个页面。

猜你喜欢

转载自blog.csdn.net/pckonline/article/details/70209758