您的位置:首页 > 编程语言 > Java开发

Hello Struts2 !

2010-02-02 10:00 169 查看
新建 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:  


2:      struts2


3:      


4:          org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter


5:  


6:   


7:  


8:      struts2


9:      /*


10:  


.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:  


2:      


3:          


4:              /index.jsp


5:          


6:      


7:  


.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:  


.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..
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: