zoukankan      html  css  js  c++  java
  • 第一个struts案例及分析

    软件中的框架,是一种半成品; 我们项目开发需要在框架的基础上进行!因为框架已经实现了一些功能,这样就可以提高开发效率!

    Struts2 = struts1  +  xwork  (struts是基于MVC模式的框架)

    struts2预先实现的功能:

      1、请求数据自动封装

      2、文件的上传

      3、国际化功能的简化

      4、数据效验

      ..........

    Struts开发步骤:

    1. web项目,引入struts - jar包

    版本: 2.3

      commons-fileupload-1.2.2.jar   【文件上传相关包】

      commons-io-2.0.1.jar

      struts2-core-2.3.4.1.jar           【struts2核心功能包】

      xwork-core-2.3.4.1.jar           【Xwork核心包】

      ognl-3.0.5.jar                          【Ognl表达式功能支持表(类似于el表达式,但语法不同)】

      commons-lang3-3.1.jar          【struts对java.lang包的扩展】

      freemarker-2.3.19.jar            【struts的标签模板库jar文件】

      javassist-3.11.0.GA.jar           【struts对字节码的处理相关jar】

    2. web.xml中,引入struts的核心功能(在项目的web.xml中引入过滤器),如下

     (struts的核心功能是通过滤器来完成初始化,Filter【init(服务器启动的时候执行)/doFilter(用户访问的时候执行)/destory(销毁执行)】,这个过滤器会过滤用户的请求,如果是以action类,该请求就会被转入struts2的框架中处理)

    <!-- 引入struts核心过滤器 -->
      <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>
    

     这个class文件的路径下的StrutsPrepareAndExecuteFilter就是一个核心过滤器(注意: 使用的struts的版本不同,核心过滤器类是不一样的!)

    3. 开发action

    (action类—动作类,取代之前的servlet,即处理请求类,action中的业务方法,处理具体的请求,一般返回string,方法中不带有参数)

    4. 配置action

             src/struts.xml,里面定义了一些列的action,也指定了这些action的实现类,并定义了这些action的处理结构与资源视图的映射关系,如下:

    <?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">
    
    <!-- START SNIPPET: xworkSample -->
    <struts>
        <package name="gqx" extends="struts-default">
        	<!-- 定义了一个test的action,具体实现com.gqx.action.HelloAction类 -->
           <action name="test" class="com.gqx.action.HelloAction" method="execute">
           		<!-- 配置action返回为success的时候,页面将跳转到success.jsp中 -->
                <result name="success">/success.jsp</result>
            </action>
        </package>
    
    </struts>
    
    <!-- END SNIPPET: xworkSample -->
    

     案例:

    1、导入响应的jar包之后。配置xml文件(web.xml)

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>firstStruts</display-name>
      
      <!-- 引入struts核心过滤器 -->
      <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>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    

     2、开发action,处理请求,通常实现actionSupport接口。

    package com.gqx.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    //开发action,处理请求
    public class HelloAction extends ActionSupport{
    	
    	//处理请求
    	@Override
    	public String execute() throws Exception {
    		// TODO Auto-generated method stub
    		System.out.println("访问到了action,正在处理请求。");
    		System.out.println("使用service方法");
    		return "success";
    	}
    }
    

     3、配置action,在src下写入struts.xml文件(和前面写的那个mystruct相似)

    <?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">
    
    <!-- START SNIPPET: xworkSample -->
    <struts>
        <package name="gqx" extends="struts-default">
           <action name="test" class="com.gqx.action.HelloAction" method="execute">
                <result name="success">/success.jsp</result>
            </action>
        </package>
    
    </struts>
    
    <!-- END SNIPPET: xworkSample -->
    

    4、在success.jsp页面写入

    <body>
        This is success page. <br>
      </body>
    

     访问写的action(根据在struts.xml中配置的信息,根据action的name去访问即可得到相对应的信息),如下,访问http://localhost:8080/firstStruts/test,页面会自动跳转到success.jsp


     struts执行流程

    一、服务器启动:

    1. 加载项目web.xml

    2. 创建Struts核心过滤器对象, 执行filter中的init(),里面有加载以下文件的方法

     (1)    struts-default.xml,    核心功能的初始化(在struts2-core-2.3.4.1.jar目录下)

    ①、通过bean节点指定struts在运行的时候创建的对象类型   

    <bean type="org.apache.struts2.components.UrlRenderer" name="struts" class="org.apache.struts2.components.ServletUrlRenderer"/>
    

    ②、指定struts包(用户写的package(struts.xml)一样要继承此包)

    <package name="struts-default" abstract="true">
            <result-types>
                <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
               .......
            </result-types>
    </package>
    

     result-types:跳转的结果类型,在struts.xml中不写type类型默认是转发(dispatcher);

            [redirectAction:是指跳转(重定向)到另外一个action]

            [stream:文件下载的时候会用到]

    ③、定义了所有的拦截器(一共32个拦截器)

     <interceptors>
                <interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>
            ..... </interceptors>

     在拦截器中为了拦截器引用方便,通过定义栈的方式引用拦截器,此时如果引用了栈,则所有的拦截器都会被使用,拦截器的执行顺序是按栈中的排列顺序执行的。

    <interceptor-stack name="defaultStack">
                    <interceptor-ref name="exception"/>
                    <interceptor-ref name="alias"/>
                    ......
     </interceptor-stack>   
    

     ④、在拦截器的后面就立刻引用了defaultStack,默认执行上面的18个拦截器栈(defaultStack栈)、默认执行的action

    <default-interceptor-ref name="defaultStack"/>
    
    <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
    

     备注:拦截器功能与过滤器类似,共同点都拦截资源,区别:过滤器拦截所有资源,拦截器只拦截action请求,拦截器只在struts中用,而过滤器可以在struts和servlet中用。

    用户访问的时候按顺序执行18个拦截器;先执行Action类的创建,在执行拦截器,拦截器执行完后,在执行action类的业务逻辑方法

    (2)struts-plugin.xml,     struts相关插件

    (3)struts.xml                 用户编写的配置文件

    二、访问,可以在HelloAction类中添加一个无参构造方法

    public HelloAction() {
    		// TODO Auto-generated constructor stub
    		System.out.println("HelloAction.HelloAction()");
    	}
    

     每当我们访问的时候可以看到控制台的信息,同时不论第一次访问,还是第二次访问,他都会调用这个无参构造方法,与servlet的一次调用不同。

    所以用户每次访问都会创建一个acton实例。


    配置详解

     总配置文件,可以包含其他的配置文件

    <?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">
    
    <!-- START SNIPPET: xworkSample -->
    <struts>
        <package name="gqx" extends="struts-default" abstract="false">
        	<!-- 定义了一个test的action,具体实现com.gqx.action.HelloAction类 -->
           <action name="test" class="com.gqx.action.HelloAction" method="execute">
           		<!-- 配置action返回为success的时候,页面将跳转到success.jsp中 -->
                <result name="success">/success.jsp</result>
            </action>
        </package>
    
    	<!-- 在总配置文件中引入其他所有的配置文件(src/struts.xml在服务器自动的时候就会执行) -->
    	<!-- package 定义一个包,包的作用:管理action(通常一个业务模板放一个包) -->
    	<include file="com/gqx/other/user.xml"></include>
    </struts>
    
    <!-- END SNIPPET: xworkSample -->
    

     被包含的配置文件(user.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">
    
    <!-- START SNIPPET: xworkSample -->
    <struts>
    	<!-- package 定义一个包,包的作用:管理action(通常一个业务模板放一个包)
    		 		 name:包的名字;包名不可重复,若重复,服务器启动的时候会报错
    		 		 extends:当前包继承自哪个包,在struts中一定要继承自struts-default,在struts-default.xml中指定的包,
    		 		 abstract:表示当前包为抽象包,抽象包中不能有action的定义,否则运行的时候会出错
    		 		 abstract:true只有当前包被其他包继承的时候使用
    		 		 namespace:名称空间,是路径的一部分,默认为"/",
    		 		  如果将默认改为"/user/",则这个action处理类访问的地址就会在http://localhost:8080/项目/名称空间(/user/)/ActionName(login)下访问
    		  action:配置请求路径与action类的映射关系
    		  	name:请求的路径名称
    		  	class:请求处理的action类的全名
    		  	type:跳转的结果类型
    		  	标签体中指定跳转的页面
    		  	
    		 namespace
    	  -->
        <package name="user" extends="struts-default" abstract="false">
            <action name="login" class="com.gqx.other.UserAction" method="login">
           		<!-- 配置action返回为success的时候,页面将跳转到success.jsp中 -->
                <result name="login">/success.jsp</result>
            </action>
        </package>
    
    </struts>
    
    <!-- END SNIPPET: xworkSample -->
    

     这里写了另外一个测试案例,其中包含了对象的封装,还有关于域对象中数据的封装

    1、写一个user的bean

    package com.gqx.other;
    
    public class User {
    	private String userName;
    	private String pwd;
    	public String getUserName() {
    		return userName;
    	}
    	public void setUserName(String userName) {
    		this.userName = userName;
    	}
    	public String getPwd() {
    		return pwd;
    	}
    	public void setPwd(String pwd) {
    		this.pwd = pwd;
    	}
    	
    }
    

     2、写一个action的类UserAction

    package com.gqx.other;
    
    import java.util.Map;
    
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    
    //开发action,处理请求
    public class UserAction extends ActionSupport{
    	private User user=new User();
    	public void setUser(User user) {
    		this.user = user;
    	}
    	public User getUser() {
    		return user;
    	}
    	
    	public String login(){
    		//获取用户的用户名和密码
    		System.out.println(user.getUserName());
    		System.out.println(user.getPwd());
    		
    		//要把数据保存到域中去
    		ActionContext ac=ActionContext.getContext();
    		//struts会处理以下域对象
    		//得到request的map
    		Map<String, Object> request=ac.getContextMap();
    		//得到session的map
    		Map<String, Object> session=ac.getSession();
    		//得到servletContext的map
    		Map<String, Object> application=ac.getApplication();
    		
    		//保存
    		request.put("rr", "哈哈哈,");
    		session.put("ss", "Hello  ");
    		application.put("aa", "World!");
    		return "login";
    	}
    }
    

     配置文件就是上面讲的配置文件,以这个为案例写的,现在去写登入页面,注意登入页面的input中的name

    <form action="${pageContext.request.contextPath }/login" name="frmLogin"  method="post">
       	   用户名: <input type="text" name="user.userName"> <br/>
       	 密码: <input type="text" name="user.pwd"> <br/>
       	   <input type="submit" value="登陆"> <br/>
    </form>
    

     然后就是index.jsp了

     This is my success page. <br>
        ${requestScope.rr }
        ${sessionScope.ss }
        ${applicationScope.aa }
    

    最后去访问,如图:


    js小练习

    qq显示的效果,这里注意索引的使用,以及this的使用

    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>QQ效果</title>
        <style type="text/css" media="screen">
        div {
            display: none;
        }
        </style>
    </head>
    
    <body>
        <ul>
            <li>
                <h3>列表1</h3>
                <div>
                    <p>pppppp</p>
                    <p>pppppp</p>
                </div>
            </li>
            <li>
                <h3>列表2</h3>
                <div>
                    <p>pppppp</p>
                    <p>pppppp</p>
                </div>
            </li>
            <li>
                <h3>列表3</h3>
                <div>
                    <p>pppppp</p>
                    <p>pppppp</p>
                </div>
            </li>
            <li>
                <h3>列表4</h3>
                <div>
                    <p>pppppp</p>
                    <p>pppppp</p>
                </div>
            </li>
        </ul>
        <script type="text/javascript">
        var h3 = document.getElementsByTagName('h3');
        var div = document.getElementsByTagName('div');
        for (var i = h3.length - 1; i >= 0; i--) {
            h3[i].index = i;
            h3[i].onoff = true;
            h3[i].onclick = function() {
                if (this.onoff) {
                    div[this.index].style.display = 'inline';
                    this.onoff = false;
                } else {
                    div[this.index].style.display = 'none';
                    this.onoff = true;
                }
            }
        }
        </script>
    </body>
    
    </html>
    
  • 相关阅读:
    反向代理与 Real-IP 和 X-Forwarded-For
    Jmeter学习笔记(十一)——定时器
    Jmeter学习笔记(十)——元件的作用域和执行顺序
    Jmeter学习笔记(九)——响应断言
    Jmeter学习笔记(八)——监听器元件之聚合报告
    Jmeter学习笔记(七)——监听器元件之察看结果树
    Jmeter学习笔记(六)——使用badboy录制脚本
    Jmeter学习笔记(五)——dubbo接口测试
    Jmeter学习笔记(四)配置元件之计数器
    jmeter学习笔记(三)配置元件之HTTP信息头管理
  • 原文地址:https://www.cnblogs.com/helloworldcode/p/6142893.html
Copyright © 2011-2022 走看看