• Structs复习 Result第二部分


    1.动态结果

    2.web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      
      <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>
    </web-app>

    3.Structs.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    <struts>
        <constant name="struts.devMode" value="true" />
        <package name="user" namespace="/user" extends="struts-default">
            
            <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
                <result>${r}</result>    <!--访问ValueStack里的值 根据传入值的不同来动态调用结果-->
            </action>        
        </package>
            
    </struts>

    4.UserAction.Java

    package com.bjsxt.struts2.user.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class UserAction extends ActionSupport {
        private int type;
        
        private String r;
    
        public String getR() {
            return r;
        }
    
        public void setR(String r) {
            this.r = r;
        }
    
        public int getType() {
            return type;
        }
    
        public void setType(int type) {
            this.type = type;
        }
    
        @Override
        public String execute() throws Exception {
            if(type == 1) r="/user_success.jsp";
            else if (type == 2) r="/user_error.jsp";
            return "success";
        }
    
    }

    index.jsp

    <?xml version="1.0" encoding="GB18030" ?>
    <%@ page language="java" contentType="text/html; charset=GB18030"
        pageEncoding="GB18030"%>
    
    <% String context = request.getContextPath(); %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
    <title>Insert title here</title>
    </head>
    <body>
    动态结果
    一定不要忘了为动态结果的保存值设置set get方法
    <ol>
        <li><a href="user/user?type=1">返回success</a></li>
        <li><a href="user/user?type=2">返回error</a></li>
    </ol>
        
    </body>
    </html>

    user_error.jsp

    <?xml version="1.0" encoding="GB18030" ?>
    <%@ page language="java" contentType="text/html; charset=GB18030"
        pageEncoding="GB18030"%>
        <%@taglib uri="/struts-tags" prefix="s" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
    <title>UserError</title>
    </head>
    <body>
        User Error!
    </body>
    </html>

    user_success.jsp

    <?xml version="1.0" encoding="GB18030" ?>
    <%@ page language="java" contentType="text/html; charset=GB18030"
        pageEncoding="GB18030"%>
        <%@taglib uri="/struts-tags" prefix="s" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
    <title>UserSuccess</title>
    </head>
    <body>
        User Success!
        <s:property value="r"/>
        <s:debug></s:debug>
    </body>
    </html>

    2.forward共享一个值栈   redirect由于是先返回客户端在访问服务器  Action 已经没有值了因此要从param里取值

    jar包

     web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      
      <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>
    </web-app>

    Struct.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    <struts>
        <constant name="struts.devMode" value="true" />
        <package name="user" namespace="/user" extends="struts-default">
            
            <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
                <result type = "redirect">http://localhost:6666///Struts2_1800_ResultWithParams/user_success.jsp?t=${type}</result>
            </action>        
        </package>
            
    </struts>

    UserAction.Java

    package com.bjsxt.struts2.user.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class UserAction extends ActionSupport {
        private int type;
        
        public int getType() {
            return type;
        }
    
        public void setType(int type) {
            this.type = type;
        }
    
        @Override
        public String execute() throws Exception {
            return "success";
        }
    
    }

    index.jsp

    <?xml version="1.0" encoding="GB18030" ?>
    <%@ page language="java" contentType="text/html; charset=GB18030"
        pageEncoding="GB18030"%>
    
    <% String context = request.getContextPath(); %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
    <title>Insert title here</title>
    </head>
    <body>
    向结果传参数
    <ol>
        <li><a href="user/user?type=1">传参数</a></li>
    </ol>
        
    </body>
    </html>

    user_success.jsp

    <?xml version="1.0" encoding="GB18030" ?>
    <%@ page language="java" contentType="text/html; charset=GB18030"
        pageEncoding="GB18030"%>
        <%@taglib uri="/struts-tags" prefix="s" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
    <title>UserSuccess</title>
    </head>
    <body>
        User Success!
        from valuestack: <s:property value="t"/><br/>    //Action里为空 没有值
        from actioncontext: <s:property value="#parameters.t"/>      //parameter里还有
        <s:debug></s:debug>
    </body>
    </html>
  • 相关阅读:
    关于linux命令 cp,rm,mv执行时是否询问的问题,不同用户有不同的别名设置
    用jquery根据json动态创建多级菜单导航(by https://www.cnblogs.com/fatty-yu/p/7088955.html)
    前台向后台传值的两种方法 以及 从后台获取数据的方法
    “加载更多”功能如何实现
    AngularJS的主要组成部分:
    数组与字符串的相互转化
    angular过滤器的使用 By http://www.tuicool.com/articles/ueUZBv
    transclude
    内嵌模版 template 原创
    导航栏选中效果 ng-repeat
  • 原文地址:https://www.cnblogs.com/frankzone/p/9468122.html
Copyright © 2020-2023  润新知