码迷,mamicode.com
首页 > 其他好文 > 详细

SSH之旅(二)——Struts2 配置文件

时间:2015-08-29 00:51:44      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

    初次接触Java SSH框架,使用MyEclipse开发工具,以及Tomcat热部署,不明白如何访问JSP页面,如何访问Action调用业务逻辑层服务。现在就从Struts配置文件开始走起!

从打开页面开始,如果访问某个id=18的商品详情页?

<span style="font-family:SimSun;font-size:14px;">http://localhost:8080/MyShop/product_findByPid.action?pid=81</span>
    MyShop是我的项目名称,".action"前是对应的action动作名。Struts2会拦截这部分字符串,在配置文件中查找product下的findByPid方法。如下Struts配置文件:
<span style="font-family:SimSun;font-size:14px;"><span style="white-space:pre">	</span></span><pre name="code" class="html"><span style="white-space:pre">	</span><!-- 商品模块的Action --> 
	<action name="product_*" class="productAction" method="{1}">
<span>		<span style="color: rgb(63, 95, 191);"><!-- </span><span style="color: rgb(63, 95, 191);">定义处理结果字符串和资源之间的映射关系</span><span style="color: rgb(63, 95, 191);"> --></span></span>
		<result name="findByPid">/WEB-INF/jsp/product.jsp</result>
	</action>


    没有系统的学过SSH原理,就搞不清这些配置、调用关系,就不会调bug,出了错也不知道是哪里的问题。下面按着树的层级形式解说。

1、package包

    Struts2框架中核心组件就是Action、拦截器等,Struts2框架使用包来管理Action和拦截器等。每个包就是多个Action、多个拦截器等。它的属性有:

    name:必填属性,用来指定包的名字。    extends:可选属性,用来指定该包继承其他包。继承其它包,可以继承其它包中的Action定义、拦截器定义等。    namespace:可选属性,用来指定该包的命名空间。

<span style="font-family:SimSun;font-size:14px;">      <package name="myShop" extends="struts-default" namespace="/"></package></span>

    浏览器地址是根据 namespace/action 名称访问的,这个namespace是虚拟路径,可以在项目中不存在。但是为了在项目中方便查找JSP页面,会在 class/jsp 路径中匹配上namespace的名。


2、命名空间配置

<span style="font-family:SimSun;font-size:14px;">      <!-- 商品模块的Action --> </span><span style="font-family: SimSun; font-size: 14px;">      </span><span style="font-family:SimSun;font-size:14px;">
      <action name="product_*" class="myShop.product.action.ProductAction" method="{1}">
      </action></span>
    这个class就是命名空间路径,不同的命名空间,可以允许同名,这些都是新建项的问题了。其实也可以通过Spring来管理action对象:

<span style="font-family:SimSun;font-size:14px;">      <!-- Spring下 商品模块的Action -->
</span><pre name="code" class="html"><span style="font-family:SimSun;font-size:14px;">       </span><span style="font-family: SimSun;"><bean id="productAction" class="myShop.product.action.ProductAction" scope="prototype"></span>
<property name="productService" ref="productService"/> </bean>


<span style="font-family:SimSun;font-size:14px;"><!-- Struts下 商品模块的Action --> 
		<action name="product_*" class="productAction" method="{1}">			
		</action></span>

   struts2单独使用时action由struts2自己负责创建;与spring集成时,action对象实例由spring负责创建,且Struts的class属性是spring的applicationContext.xml中配置的bean的id属性值。


3、为Action配置method属性:

    method=”{1}“表示调用通配符的第一个,把传来的通配符和下边的字符串找共同。

4、配置处理结果:

    Struts2通过在struts.xml文件中使用<result …/>元素来配置结果。Struts2提供了两种结果配置。

    局部结果:将<result …/>作为<action …>元素的子元素配置。
    全局结果:将<result …/>作为<global-results …>元素的子元素配置。


    总之,struts.xml文件主要负责管理应用中的Action映射, 及Action处理结果和物理资源之间的映射关系。而对于Struts2中的Action,就是Controller控制器,由Action作为一个枢纽,继承ActionSupport类,实现ModelDrive模型驱动,注入各种属性,生成 get/set 方法,调用Service。



版权声明:本文为博主原创文章,未经博主允许不得转载。

SSH之旅(二)——Struts2 配置文件

标签:

原文地址:http://blog.csdn.net/u010096526/article/details/48063713

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!