• 02 Struts2框架----学习笔记2(了解一下,已过时)


    1.*号通配符优化struts.xml代码

    创建一个UserAction的动作类

    package action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class UserAction extends ActionSupport{
    
        //添加用户
        public String add(){
            return SUCCESS;
        }
        //删除用户
        public String delete(){
            return SUCCESS;
        }
        //更新用户
        public String update(){
            return SUCCESS;
        }
        
        //查找用户
        public String find(){
            return SUCCESS;
        }
        
    }

    Struts.xml文件配置

    <!--第一种配置方式-->
        <!--<package name="p2" extends="struts-default">
            <action name="add" class="action.UserAction" method="add">
                <result name="success">/add.jsp</result>
            </action>
    
            <action name="delete" class="action.UserAction" method="delete">
                <result name="success">/delete.jsp</result>
            </action>
    
            <action name="find" class="action.UserAction" method="find">
                <result name="success">/find.jsp</result>
            </action>
    
            <action name="update" class="action.UserAction" method="update">
                <result name="success">/update.jsp</result>
            </action>
        </package>-->
    
        <!--采用通配符的方式-->
        <!-- 动作访问之通配符
               * :这里是一个*-->
        <package name="p2" extends="struts-default">
            <action name="*" class="action.UserAction" method="{1}">
                <result name="success">/{1}.jsp</result>
            </action>
        </package>

     2.Struts.xml配置文件中的result元素的type取值的四种类型

    chain的配置实例:

    <1>利用chain转发到同一个包下的不同action

    <package name="p3" extends="struts-default">
            <action name="hello1" class="action.HelloAction" method="sayHello">
                <result name="success" type="chain">hello2</result>
            </action>
    
            <action name="hello2" class="action.HelloAction2" method="sayHello2">
                <result name="success">/hello2.jsp</result>
            </action>
        </package>

    效果图:

    <2>利用chain转发到不同包下的不同action

    <package name="p3" extends="struts-default">
            <action name="hello1" class="action.HelloAction" method="sayHello">
                <result name="success" type="chain">hello3</result>
            </action>
    
            <action name="hello2" class="action.HelloAction2" method="sayHello2">
                <result name="success">/hello2.jsp</result>
            </action>
        </package>
    
        <package name="p4" extends="struts-default">
    
            <action name="hello3" class="action.HelloAction3" method="sayHello3">
                <result name="success">/hello3.jsp</result>
            </action>
        </package>

    注意:若加上namespace属性,则struts.xml文件的配置应修改为:

    <package name="p3" extends="struts-default" namespace="/n3">
            <action name="hello1" class="action.HelloAction" method="sayHello">
                <result name="success" type="chain">
                    <param name="namespace">/n4</param>
                    <param name="actionName">hello3</param>
                </result>
            </action>
    
            <action name="hello2" class="action.HelloAction2" method="sayHello2">
                <result name="success">/hello2.jsp</result>
            </action>
        </package>
    
        <package name="p4" extends="struts-default" namespace="/n4">
    
            <action name="hello3" class="action.HelloAction3" method="sayHello3">
                <result name="success">/hello3.jsp</result>
            </action>
        </package>

    效果图:

     3.动作类中的servlet api访问

    通过ServletActionContext获取获取servletapi

    package action;
    
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class TestAction extends ActionSupport{
    
        public String test(){
            //获取servlet 的api方式
            //第一种方式:通过ServletActionContext
            //1.response
            HttpServletResponse response = ServletActionContext.getResponse();
            
            //2.request
            HttpServletRequest request = ServletActionContext.getRequest();
            
            //3.session
            HttpSession session =  request.getSession();
            
            //4.application【ServletContext】
            ServletContext application = ServletActionContext.getServletContext();
            
    
            //NONE不跳转,也就是不用找result标签
            return NONE;
        }
    }

    4.Action接收请求参数的方式

    (1)通过request对象

         //获取请求参数
            //第一种方法:通过request对象
            HttpServletRequest request =  ServletActionContext.getRequest();
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            
            System.out.println(username);
            System.out.println(password);

    (2)第2种方式:通过属性的set注入

    package action;
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletRequest;
    import javax.xml.ws.RequestWrapper;
    
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class LoginAction extends ActionSupport{
    
        private String username;
        private String password;
        
        
        public void setUsername(String username) {
            System.out.println("setUsername");
            this.username = username;
        }
    
    
        public void setPassword(String password) {
            System.out.println("setPassword");
            this.password = password;
        }
        
        public String login(){
    
            //获取请求参数
            /*//第一种方法:通过request对象
            HttpServletRequest request =  ServletActionContext.getRequest();
            String username = request.getParameter("username");
            String password = request.getParameter("password");*/
    
            System.out.println(username);
            System.out.println(password);
    
            //第二种方式:通过属性的set注入
            
            //NONE不跳转,也就是不用找result标签
            return NONE;
        }
    }

     (3)第3种方式:通过模型驱动(最常用)

    创建一个model---->User

    package model;
    
    public class User {
    
        private String username;
        private String password;
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        @Override
        public String toString() {
            return "User [username=" + username + ", password=" + password + "]";
        }
        
        
        
    }

    动作类代码:

    package action;
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletRequest;
    import javax.xml.ws.RequestWrapper;
    
    import com.opensymphony.xwork2.ModelDriven;
    import model.User;
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class LoginAction extends ActionSupport implements ModelDriven<User> {
    
        private User user = new User();
        @Override
        public User getModel() {
            // TODO Auto-generated method stub
            return user;
        }
    
        public String login(){
    
            //获取请求参数
            /**
             * 第三种方式: 通过模型驱动【最常用】
             * 实现步骤:
             * 1.在action里实现一个模型驱动接口
             * 2.提供一个模型属性,并一定要赋值
             * 通过这种方式,在jsp页面中就不用写user.
             *
             * 这是模型驱动的原理是因为有个模型驱动的拦截器在处理
             * 这个类就com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor
             */
            System.out.println(user);
            
            //NONE不跳转,也就是不用找result标签
            return NONE;
        }
    }

    控制台输出:

  • 相关阅读:
    my first android test
    VVVVVVVVVV
    my first android test
    my first android test
    my first android test
    ini文件
    ZZZZ
    Standard Exception Classes in Python 1.5
    Python Module of the Week Python Module of the Week
    my first android test
  • 原文地址:https://www.cnblogs.com/luckyplj/p/11293897.html
Copyright © 2020-2023  润新知