因为最近要做一个项目,最后语言选择的是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配制文件的例子:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app id="WebApp_ID" version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation=" /web-app_2_4.xsd">
-
<display-name>Zhuanhuan_Demo</display-name>
-
-
<filter>
-
<filter-name>struts2</filter-name>
-
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
-
<init-param>
-
<param-name>struts.action.extension</param-name>
-
<param-value>action</param-value>
-
</init-param>
-
</filter>
-
-
<filter-mapping>
-
<filter-name>struts2</filter-name>
-
<url-pattern>/*</url-pattern>
-
-
</filter-mapping>
-
-
<welcome-file-list>
-
<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>
三:有了上面的那个配制文件,在我们访问程序时,项目流程已经进入了struts,只是我们现在什么也没有做,下面要进行struts的配制,文件名称就是叫做:struts.xml,把这个文件配制到java Resoureces项目目录下的src根目录下即可:
四:在使用struts之前我们先了解一下struts的配制文件,也就是struts.xml文件,他的文件标准如下:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<!--
-
/*
-
* $Id$
-
*
-
* Licensed to the Apache Software Foundation (ASF) under one
-
* or more contributor license agreements. See the NOTICE file
-
* distributed with this work for additional information
-
* regarding copyright ownership. The ASF licenses this file
-
* to you under the Apache License, Version 2.0 (the
-
* "License"); you may not use this file except in compliance
-
* with the License. You may obtain a copy of the License at
-
*
-
* http://www.apache.org/licenses/LICENSE-2.0
-
*
-
* Unless required by applicable law or agreed to in writing,
-
* software distributed under the License is distributed on an
-
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-
* KIND, either express or implied. See the License for the
-
* specific language governing permissions and limitations
-
* under the License.
-
*/
-
-->
-
<!-- START SNIPPET: strutsDtd -->
-
-
<!--
-
Struts configuration DTD.
-
Use the following DOCTYPE
-
-
<!DOCTYPE struts PUBLIC
-
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
-
"">
-
-->
-
-
<!ELEMENT struts (package|include|bean|constant)*>
-
-
<!ELEMENT package (result-types?, interceptors?, default-interceptor-ref?, default-action-ref?, default-class-ref?, global-results?, global-exception-mappings?, action*)>
-
<!ATTLIST package
-
name CDATA #REQUIRED
-
extends CDATA #IMPLIED
-
namespace CDATA #IMPLIED
-
abstract CDATA #IMPLIED
-
externalReferenceResolver NMTOKEN #IMPLIED
-
>
-
-
<!ELEMENT result-types (result-type+)>
-
-
<!ELEMENT result-type (param*)>
-
<!ATTLIST result-type
-
name CDATA #REQUIRED
-
class CDATA #REQUIRED
-
default (true|false) "false"
-
>
-
-
<!ELEMENT interceptors (interceptor|interceptor-stack)+>
-
-
<!ELEMENT interceptor (param*)>
-
<!ATTLIST interceptor
-
name CDATA #REQUIRED
-
class CDATA #REQUIRED
-
>
-
-
<!ELEMENT interceptor-stack (interceptor-ref*)>
-
<!ATTLIST interceptor-stack
-
name CDATA #REQUIRED
-
>
-
-
<!ELEMENT interceptor-ref (param*)>
-
<!ATTLIST interceptor-ref
-
name CDATA #REQUIRED
-
>
-
-
<!ELEMENT default-interceptor-ref (#PCDATA)>
-
<!ATTLIST default-interceptor-ref
-
name CDATA #REQUIRED
-
>
-
-
<!ELEMENT default-action-ref (#PCDATA)>
-
<!ATTLIST default-action-ref
-
name CDATA #REQUIRED
-
>
-
-
<!ELEMENT default-class-ref (#PCDATA)>
-
<!ATTLIST default-class-ref
-
class CDATA #REQUIRED
-
>
-
-
<!ELEMENT global-results (result+)>
-
-
<!ELEMENT global-exception-mappings (exception-mapping+)>
-
-
<!ELEMENT action (param|result|interceptor-ref|exception-mapping)*>
-
<!ATTLIST action
-
name CDATA #REQUIRED
-
class CDATA #IMPLIED
-
method CDATA #IMPLIED
-
converter CDATA #IMPLIED
-
>
-
-
<!ELEMENT param (#PCDATA)>
-
<!ATTLIST param
-
name CDATA #REQUIRED
-
>
-
-
<!ELEMENT result (#PCDATA|param)*>
-
<!ATTLIST result
-
name CDATA #IMPLIED
-
type CDATA #IMPLIED
-
>
-
-
<!ELEMENT exception-mapping (#PCDATA|param)*>
-
<!ATTLIST exception-mapping
-
name CDATA #IMPLIED
-
exception CDATA #REQUIRED
-
result CDATA #REQUIRED
-
>
-
-
<!ELEMENT include (#PCDATA)>
-
<!ATTLIST include
-
file CDATA #REQUIRED
-
>
-
-
<!ELEMENT bean (#PCDATA)>
-
<!ATTLIST bean
-
type CDATA #IMPLIED
-
name CDATA #IMPLIED
-
class CDATA #REQUIRED
-
scope CDATA #IMPLIED
-
static CDATA #IMPLIED
-
optional CDATA #IMPLIED
-
>
-
-
<!ELEMENT constant (#PCDATA)>
-
<!ATTLIST constant
-
name CDATA #REQUIRED
-
value CDATA #REQUIRED
-
>
-
-
<!-- END SNIPPET: strutsDtd -->
从他的DTD结构文件当中可以了解到struts配制文件根元素无非就是这几个:
<!ELEMENT struts (package|include|bean|constant)*>.....
如果不懂DTD文件,请自行百度学习
下面是一个struts.xml配制文件的框架:
-
<!DOCTYPE struts PUBLIC
-
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
-
"">
-
<struts>
-
<package name="main" extends="struts-default">
-
-
</package>
-
</struts>
通过上面的简单的介绍,可以大概的了解struts框架配制流程,下面通过一个程序(用户登陆来更加具体的了解struts框架的使用和他的执行流程)
阅读(678) | 评论(0) | 转发(0) |