Hello Struts2 !

来源:互联网 发布:淘宝客流量是什么意思 编辑:程序博客网 时间:2024/05/15 02:52

新建 JAVA WEB 项目 StrutsTest

拷贝 example Apps 里面的 struts.xml 到 MyEclipse StrutsTest 项目里面的  src 目录下,拷贝 example Apps 里面的 JAR 包到 WebRoot/WEB-INF/lib 目录下,打开 MyEclipse StrutsTest 项目里面的 WebRoot/WEB-INF 目录下的 web.xml ,参照 example Apps 里面的 web.xml 添加 filter 代码:

   1:  <filter>
   2:      <filter-name>struts2filter-name>
   3:      <filter-class>
   4:          org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
   5:  filter>
   6:   
   7:  <filter-mapping>
   8:      <filter-name>struts2filter-name>
   9:      <url-pattern>/*url-pattern>
  10:  filter-mapping>
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }

标签里面的 /* 一般不需要改动 ( /* 是指对全部地址进行过滤 ),或者是改动到这个值。

在 struts.xml 的 标签中插入如下代码:

   1:  <package name="default" namespace="/" extends="struts-default">
   2:      <action name="hello">
   3:          <result>
   4:              /index.jsp
   5:          result>
   6:      action>
   7:  package>
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }

在通过一下代码设定开发者模式:

   1:  <constant name="struts.devMode" value="true" />
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }

重启服务器后,在浏览器访问 localhost/StrutsTest/hello 即可访问 index.jsp。

访问原理分析:

当我们在浏览器中输入 localhost/StrutsTest/hello ,Tomcat 会根据此 URL 读取 StructsTest 目录中的 web.xml ,此 web.xml 里面配置了过滤器 filter ,此过滤器将所有地址访问都转给了 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter ,这个 StrutsPrepareAndExecuteFilter 类分析 URL ,得到 namespace 为 / ,然后定位到 namespace 为 / 的 package ( 当 namespace 为空的时候能匹配所有 action ),调用里面的 action ,配对到 index.jsp。

有 Hello World 情节的童鞋,可以在修改 index.jsp 内容为 Hello World,或者 Hello Struts2..

原创粉丝点击