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>
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>
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";
}
}
8.測試
輸入http://localhost:8080/TestMvc/add.JSP
輸入123 和 456
結果
結束!
不好意思,實在是寫的太簡單了,改日分析一下,呵呵,睡覺了