strtus2.0 helloworld

一、helloworld示例:

 

1. 新建web工程,解压strutsstruts2-core-2.1.x 。在其中找到struts.xml放进src下,2.1.6\apps\struts2-blank- 

   2.1.6下的struts.xml。 然后在lib下拷贝所有的jar除了junit和sprint-test放入工程lib下。

2.在web.xml下添加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>

    这里是固定写法,约定俗成,不需要改动。

3.注释掉struts.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.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />

    <include file="example.xml"/>



    <package name="default" namespace="/" extends="struts-default">
        <default-action-ref name="index" />
        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>
	 -->
    <!-- Add packages here -->
      
</struts>

 在其中加入如下配置,来完成我们的第一个helloworld:

<constant name="struts.devMode" value="true" />
	  <package name="default" namespace="/" extends="struts-default">
        <action name="hello">
            <result>
                /hello.jsp
            </result>
        </action>
    </package>

  在webroot下建立hello.jsp,在web browser中输入:http://localhost:8080/projectName/hello.action

  就会跳转到hello.jsp。第一个helloworld就完成了。

 

解释一下struts.xml:

<constant name="struts.devMode" value="true" />
开发模式,设为true后,修改了srtus.xml后不用每次都重启tomcat了。

namespace="/",是跟路径有关的,extends用它默认的就行。

4.在Eclipse中加入对struts2xml的标签提示:

windows > preference  > 搜xml > xml Catalog > Add  > key type:URI ,  key: http://struts.apache.org/dtds/struts-2.0.dtd ,  localcation : 指定dtd文件的路径 > OK >重新打开struts.xml.

 

二、运行机制说明:

http://localhost:8080/projectName/hello.action 的http请求request后,tomcat服务器收到该请求,从projectName知道了客户端要请求的webapps,所以进该webapps中参考web.xml,我们在web.xml中配过一个filter,所有的/*的请求都会交给filter处理,filter会参考strtus.xml,找到对应的namespace、action、result,找到result后,filter将这个请求forward到result对应的jsp文件,然后jsp把页面response给客户端。

 

三、为什么要使用strtus:

将请求拦截做处理,解析请求,根据struts写好的配置,分发到不同的页面,这样带来的好处是灵活性比较高,要修改跳转jsp页面,字需要修改result节点就行了。strtus还实现了业务逻辑(Model)与视图(View)的分离,业务逻辑不再混杂在jsp中,全部写到action中就行了,action起到了控制器(controller)的作用,这就是我们常说的MVC框架的实现。

、对strtus2.2.1.1:

目前apache官方发布的最新struts版本是2.2.1.1,搭建环境跟2.0基本一致。

http://wenku.baidu.com/view/746440ee5ef7ba0d4a733bfd.html 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值