struts2已经是出来好久了,觉得有必要对.NET与java的MVC模式进行以下比较:
先谈下struts2的原理,不过先来入门吧
1.先安装eclipse,我只会eclipse.
2.安装jdk,tomcat
3.下载struts2的库
4.新建一个eclipse java工程

选择tomcat project

输入工程名称:TestMvc

点击完成
5.引入struts库文件

复制这些文件到,相应目录,如下:

引入库之后,需要把它们列入编译路径中:


点add jar

把他们全部加入到编译环境中
6.搭建web.xml和struts.xml(为什么以后解释)
目录结构如下:(千万不能搞错了)

web.xml

<?
xmlversion="1.0"encoding="GBK"
?>
<
web-app
xmlns
="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version
="2.4"
>
<
display-name
>
Struts2.0Sample
</
display-name
>
<
filter
>
<
filter-name
>
action2
</
filter-name
>
<
filter-class
>
org.apache.struts2.dispatcher.FilterDispatcher
</
filter-class
>
</
filter
>
<
filter-mapping
>
<
filter-name
>
action2
</
filter-name
>
<
url-pattern
>
/*
</
url-pattern
>
</
filter-mapping
>
</
web-app
>
struts.xml
<?
xmlversion="1.0"encoding="GBK"
?>
<!
DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"
>
<
struts
>
<
include
file
="struts-default.xml"
/>
<
package
name
="struts2"
extends
="struts-default"
>
<
action
name
="add"
class
="edisundong.AddAction"
>
<
result
>
add.jsp
</
result
>
</
action
>
</
package
>
</
struts
>
7.AddAction.java
packageedisundong;
importcom.opensymphony.xwork2.ActionSupport;

public
class
AddActionextendsActionSupport
...
{
/**//**
*
*/
privatestaticfinallongserialVersionUID=1L;
privateintone;
privateintanother;
privateintsum;

publicintgetAnother()...{
returnanother;
}

publicvoidsetAnother(finalintanother)...{
this.another=another;
}

publicintgetOne()...{
returnone;
}

publicvoidsetOne(finalintone)...{
this.one=one;
}

publicintgetSum()...{
returnsum;
}

publicvoidsetSum(finalintsum)...{
this.sum=sum;
}

publicStringexecute()throwsException...{
this.sum=this.one+this.another;
return"success";
}
}
8.测试
输入http://localhost:8080/TestMvc/add.jsp

输入123 和 456
结果

结束!
不好意思,实在是写的太简单了,改日分析一下,呵呵,睡觉了
本文介绍如何使用 Struts2 构建 MVC 模型。从安装 Eclipse 和 JDK 到配置 Tomcat 和 Struts2 库,再到创建工程并设置 web.xml 和 struts.xml 文件,最后通过 AddAction 类实现简单的加法操作。
6734

被折叠的 条评论
为什么被折叠?



