.NET与java的MVC模式(1):struts2入门
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
<?xml version="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/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<display-name>Struts 2.0 Sample</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>
<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/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<display-name>Struts 2.0 Sample</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
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.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>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.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
package edisundong;
import com.opensymphony.xwork2.ActionSupport;
public class AddAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private int one;
private int another;
private int sum;
public int getAnother() {
return another;
}
public void setAnother(final int another) {
this.another = another;
}
public int getOne() {
return one;
}
public void setOne(final int one) {
this.one = one;
}
public int getSum() {
return sum;
}
public void setSum(final int sum) {
this.sum = sum;
}
public String execute() throws Exception {
this.sum = this.one + this.another;
return "success";
}
}
import com.opensymphony.xwork2.ActionSupport;
public class AddAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private int one;
private int another;
private int sum;
public int getAnother() {
return another;
}
public void setAnother(final int another) {
this.another = another;
}
public int getOne() {
return one;
}
public void setOne(final int one) {
this.one = one;
}
public int getSum() {
return sum;
}
public void setSum(final int sum) {
this.sum = sum;
}
public String execute() throws Exception {
this.sum = this.one + this.another;
return "success";
}
}
8.测试
输入http://localhost:8080/TestMvc/add.jsp
输入123 和 456
结果
结束!
不好意思,实在是写的太简单了,改日分析一下,呵呵,睡觉了