• Struts2学习第七课 result


    result 是action节点的子节点

    result 代表action方法执行后,可能去的一个目的地

    一个action节点可以配置多个result子节点。

    result的name属性值对应着action方法可能有的一个返回值。

    result有两个属性,还有一个是type它表示结果的响应类型

    result的type属性值在struts-default包的result-types节点的name属性中定义,

    常用的有:

    dispatcher(默认值):转发,同Servlet中的转发。

    redirect:重定向

    redirectAction重定向到一个Action,

    注意:通过redirect的响应类型也可以便捷的实现redirectAction

    chain:转发到一个Action

    注意:不能通过redirect的响应类型也可以便捷的实现转发到一个Action

    看代码:

    struts.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
        <!-- 配置Struts2可以受理的请求的扩展名 -->
        <constant name="struts.action.extension" value="action,do"></constant>
        <package name="default" namespace="/" extends="struts-default">
            <action name="login-ui">
                <result>/login.jsp</result>
            </action>
            
            <action name="user-login" class="logan.struts.study.UserAction">
                <result name="login-success">/login-success.jsp</result>
            </action>
            
            <action name="logout" class="logan.struts.study.UserAction"
            method="logout">
                <result name="logout-success">/login.jsp</result>
            </action>
            
            <action name="testResult" class="logan.struts.study.TestResultAction"
            >
                <result name="success">/success.jsp</result>
                <!-- 重定向到一个Action -->
                <result name="index" type="redirectAction">
                    <param name="actionName">testAction</param>
                    <param name="namespace">/logan</param>
                </result>
                
                <!-- 通过redirect的响应类型也可以便捷的实现redirectAction 
                <result name="index" type="redirect">/logan/testAction.do</result>
                 -->
                <result name="login" type="redirect">/login.jsp</result>
                <result name="test" type="chain">
                    <param name="actionName">testAction</param>
                    <param name="namespace">/logan</param>
                
                </result>
            </action>
        
        </package>
        <package name="testPackage" namespace="/logan" extends="struts-default">
            <action name="testAction" class="logan.struts.study.TestAction">
                <result>/success.jsp</result>
            </action>
        </package>
        
    </struts>
    package logan.struts.study;
    
    public class TestResultAction {
        private int number;
        
        public void setNumber(int number) {
            this.number = number;
        }
        
        public String execute(){
            
            String result = null;
            
            if(number%4 == 0){
                result = "success";
            }else if(number%4 == 1){
                result = "login";
            }else if(number%4 == 2){
                System.out.println("2");
                result = "index";
            }else if(number%4 == 3){
                result = "test";
            }
            
            return result;
        }
    
    }
    package logan.struts.study;
    
    public class TestAction {
        
        public String execute(){
            System.out.println("TestAction's execute...");
            return "success";
        }
    
    }
    package logan.struts.study;
    
    import java.util.Map;
    
    import org.apache.struts2.dispatcher.SessionMap;
    import org.apache.struts2.interceptor.ApplicationAware;
    import org.apache.struts2.interceptor.SessionAware;
    
    public class UserAction implements SessionAware,ApplicationAware{
        
        private Map<String, Object> session = null;
        private Map<String, Object> application = null;
        
        private String username;
        
        public void setUsername(String username) {
            this.username = username;
        }
        
        public String logout(){
            //数量减一
            Integer count = (Integer) application.get("count");
            if(count != null && count >0){
                count--;
                application.put("count", count);
            }
            count--;
            
            //session失效
            ((SessionMap)session).invalidate();
            
            return "logout-success";
        }
        
        public String execute(){
            
            //把用户信息存入Session域中
            
            //1.获取session,通过实现RequestAware接口
            
            //获取登录信息
            session.put("username", username);
            //把用户信息存入Session域中
            
            //
            Integer count = (Integer) application.get("count");
            if(count == null){
                count = 0;
            }
            //2.使当前在线人数 + 1
            count++;
            application.put("count", count);
            
            
            return "login-success";
        }
    
        @Override
        public void setSession(Map<String, Object> session) {
            // TODO Auto-generated method stub
            this.session = session;
            
        }
    
        @Override
        public void setApplication(Map<String, Object> application) {
            // TODO Auto-generated method stub
            this.application = application;
            
        }
    
    }

    index.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <a href="login-ui.do">LoginUI</a>
        <br><br>
        <a href="testResult.do?number=4">Test ActionSupport</a>
    </body>
    </html>

    login-success.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        Welcome:${sessionScope.username }
        <br><br>
        Count on:${applicationScope.count }
        <br><br>
        <a href="logout.do">Logout</a>
    </body>
    </html>

    login.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <form action="user-login.do" method="post">
            username:<input type="text" name="username">
            <input type="submit" value="Login">
        </form>
    </body>
    </html>

    success.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <h4>Success Page</h4>
    </body>
    </html>
  • 相关阅读:
    《测试工作量的时间评估》方案梳理
    GitHub 生成密钥
    Jenkins+Jmeter持续集成(五、Ant+GitLab持续构建)
    Linux下查看文件和文件夹大小
    Java Runtime.exec()的使用
    如何启动/停止/重启MySQL
    浅析Java语言慢的原因
    chattr命令锁定账户敏感文件
    SOAP协议初级指南 (三)
    SOAP协议初级指南 (二)
  • 原文地址:https://www.cnblogs.com/LoganChen/p/6973401.html
Copyright © 2020-2023  润新知