Chinaunix首页 | 论坛 | 博客
  • 博客访问: 304862
  • 博文数量: 93
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 830
  • 用 户 组: 普通用户
  • 注册时间: 2016-02-25 10:44
个人简介

一杯茶,一台电脑

文章分类

全部博文(93)

文章存档

2018年(4)

2017年(57)

2016年(32)

分类: Java

2016-10-30 16:49:54

因为最近要做一个项目,最后语言选择的是J2EE为项目编程语言,使用的框架也是行业经典SSH框架,这系列的文章也是我学习SSH的过程,当中有不对的地方还请各位指教。
资源列表:
          下载密码:访问密码:4qRjds

    struts2.X框架这里就不介绍了,自己百度补脑;
    一:下载struts2.X的框架包,我这里已经打包了,大家可以直接下载,builderpath进自己的项目当中去;

    二:将struts应用到我们的程序当中去:
               我们知道在Servlet当中,Filter过滤器总是在Servlet之前执行,所以要使用struts就得配制一个过滤器,而这个过滤器我们不用去实现,struts已经为我们实现了,我们要做的就是把struts的这个过滤器(org.apache.struts2.dispatcher.FilterDispatcher)配制到我们的web.xml当中去,当配制完成后,我们的请求就会直接转交给struts来处理。
               下面下web.xml配制文件的例子:
          

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app id="WebApp_ID" version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation=" /web-app_2_4.xsd">
  3.     <display-name>Zhuanhuan_Demo</display-name>
  4.     
  5.     <filter>
  6.         <filter-name>struts2</filter-name>
  7.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  8.         <init-param>
  9.             <param-name>struts.action.extension</param-name>
  10.             <param-value>action</param-value>
  11.         </init-param>
  12.     </filter>
  13.     
  14.     <filter-mapping>
  15.         <filter-name>struts2</filter-name>
  16.         <url-pattern>/*</url-pattern

  17.     </filter-mapping>
  18.     
  19.     <welcome-file-list>
  20.         <welcome-file>index.htm</welcome-file>
  21.         <welcome-file>index.jsp</welcome-file>
  22.         <welcome-file>default.html</welcome-file>
  23.         <welcome-file>default.htm</welcome-file>
  24.         <welcome-file>default.jsp</welcome-file>
  25.     </welcome-file-list>
  26. </web-app>
     三:有了上面的那个配制文件,在我们访问程序时,项目流程已经进入了struts,只是我们现在什么也没有做,下面要进行struts的配制,文件名称就是叫做:struts.xml,把这个文件配制到java Resoureces项目目录下的src根目录下即可:

     四:在使用struts之前我们先了解一下struts的配制文件,也就是struts.xml文件,他的文件标准如下:

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. /*
  4.  * $Id$
  5.  *
  6.  * Licensed to the Apache Software Foundation (ASF) under one
  7.  * or more contributor license agreements. See the NOTICE file
  8.  * distributed with this work for additional information
  9.  * regarding copyright ownership. The ASF licenses this file
  10.  * to you under the Apache License, Version 2.0 (the
  11.  * "License"); you may not use this file except in compliance
  12.  * with the License. You may obtain a copy of the License at
  13.  *
  14.  * http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * Unless required by applicable law or agreed to in writing,
  17.  * software distributed under the License is distributed on an
  18.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  19.  * KIND, either express or implied. See the License for the
  20.  * specific language governing permissions and limitations
  21.  * under the License.
  22.  */
  23. -->
  24. <!-- START SNIPPET: strutsDtd -->

  25. <!--
  26.    Struts configuration DTD.
  27.    Use the following DOCTYPE
  28.    
  29.    <!DOCTYPE struts PUBLIC
  30.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  31.     "">
  32. -->

  33. <!ELEMENT struts (package|include|bean|constant)*>

  34. <!ELEMENT package (result-types?, interceptors?, default-interceptor-ref?, default-action-ref?, default-class-ref?, global-results?, global-exception-mappings?, action*)>
  35. <!ATTLIST package
  36.     name CDATA #REQUIRED
  37.     extends CDATA #IMPLIED
  38.     namespace CDATA #IMPLIED
  39.     abstract CDATA #IMPLIED
  40.     externalReferenceResolver NMTOKEN #IMPLIED
  41. >

  42. <!ELEMENT result-types (result-type+)>

  43. <!ELEMENT result-type (param*)>
  44. <!ATTLIST result-type
  45.     name CDATA #REQUIRED
  46.     class CDATA #REQUIRED
  47.     default (true|false) "false"
  48. >

  49. <!ELEMENT interceptors (interceptor|interceptor-stack)+>

  50. <!ELEMENT interceptor (param*)>
  51. <!ATTLIST interceptor
  52.     name CDATA #REQUIRED
  53.     class CDATA #REQUIRED
  54. >

  55. <!ELEMENT interceptor-stack (interceptor-ref*)>
  56. <!ATTLIST interceptor-stack
  57.     name CDATA #REQUIRED
  58. >

  59. <!ELEMENT interceptor-ref (param*)>
  60. <!ATTLIST interceptor-ref
  61.     name CDATA #REQUIRED
  62. >

  63. <!ELEMENT default-interceptor-ref (#PCDATA)>
  64. <!ATTLIST default-interceptor-ref
  65.     name CDATA #REQUIRED
  66. >

  67. <!ELEMENT default-action-ref (#PCDATA)>
  68. <!ATTLIST default-action-ref
  69.     name CDATA #REQUIRED
  70. >

  71. <!ELEMENT default-class-ref (#PCDATA)>
  72. <!ATTLIST default-class-ref
  73.     class CDATA #REQUIRED
  74. >

  75. <!ELEMENT global-results (result+)>

  76. <!ELEMENT global-exception-mappings (exception-mapping+)>

  77. <!ELEMENT action (param|result|interceptor-ref|exception-mapping)*>
  78. <!ATTLIST action
  79.     name CDATA #REQUIRED
  80.     class CDATA #IMPLIED
  81.     method CDATA #IMPLIED
  82.     converter CDATA #IMPLIED
  83. >

  84. <!ELEMENT param (#PCDATA)>
  85. <!ATTLIST param
  86.     name CDATA #REQUIRED
  87. >

  88. <!ELEMENT result (#PCDATA|param)*>
  89. <!ATTLIST result
  90.     name CDATA #IMPLIED
  91.     type CDATA #IMPLIED
  92. >

  93. <!ELEMENT exception-mapping (#PCDATA|param)*>
  94. <!ATTLIST exception-mapping
  95.     name CDATA #IMPLIED
  96.     exception CDATA #REQUIRED
  97.     result CDATA #REQUIRED
  98. >

  99. <!ELEMENT include (#PCDATA)>
  100. <!ATTLIST include
  101.     file CDATA #REQUIRED
  102. >

  103. <!ELEMENT bean (#PCDATA)>
  104. <!ATTLIST bean
  105.     type CDATA #IMPLIED
  106.     name CDATA #IMPLIED
  107.     class CDATA #REQUIRED
  108.     scope CDATA #IMPLIED
  109.     static CDATA #IMPLIED
  110.     optional CDATA #IMPLIED
  111. >

  112. <!ELEMENT constant (#PCDATA)>
  113. <!ATTLIST constant
  114.     name CDATA #REQUIRED
  115.     value CDATA #REQUIRED
  116. >

  117. <!-- END SNIPPET: strutsDtd -->
从他的DTD结构文件当中可以了解到struts配制文件根元素无非就是这几个:
<!ELEMENT struts (package|include|bean|constant)*>.....

如果不懂DTD文件,请自行百度学习
下面是一个struts.xml配制文件的框架:

点击(此处)折叠或打开

  1. <!DOCTYPE struts PUBLIC
  2.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  3.     "">
  4. <struts>
  5.     <package name="main" extends="struts-default">
  6.        
  7.     </package>
  8. </struts>

    通过上面的简单的介绍,可以大概的了解struts框架配制流程,下面通过一个程序(用户登陆来更加具体的了解struts框架的使用和他的执行流程)


    
    

  
阅读(678) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~