或許有人覺得struts不容易學,似乎里面的一些概念讓未接觸過的人迷惑,mvc1、mvc2、模式……我寫這篇文章是想讓從來沒有接觸過struts的人,能有個簡單的入門指引,當然,系統地學習struts是必要的,里面有很多讓人心醉的東東,那是后話了。
該案例包括首頁,用戶登陸、網站向導頁面。就這么簡單,沒有深奧的struts概念,主要靠動手,然后用心體會。
web server用tomcat4。到 http://jakarta.apache.org 下載struts1.1,把zip文件釋放到c:/struts,拷貝c:/struts/webapps/struts-example.war到c:/tomcat4/webapps中,啟動tomcat,war包被釋放為struts-example文件夾,刪除war包,把struts-example文件夾更名為test。
一、把web-inf/web.xml改成:
<?xml version="1.0" encoding="iso-8859-1"?>
<!doctype web-app public "-//sun microsystems, inc.//dtd web application 2.2//en" " http://java.sun.com/j2ee/dtds/web-app_2_2.dtd ">
<web-app>
<!—這是struts中的controller(控制器),系統的指令中轉由其,既actionservlet 類負責,它從struts-config.xml中讀取配置信息,并在服務器后臺自動啟動一個線程。如果沒有特別的要求(如添加語言編轉功能),程序員可以不管這部分,照用就可以了。-->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.actionservlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/web-inf/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--該系統的servlet可以映射成cool為后綴的文件,而不是常見的.jspdo等,后綴名可以改成任何名稱,當然名字要健康#◎¥%!-->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.cool</url-pattern>
</servlet-mapping>
<!--該系統的默認首頁是index.jsp,可以有多個,系統按次序找,類似iis-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
二、把test/web-inf/ struts-config.xml改成:
<?xml version="1.0" encoding="iso-8859-1" ?>
<!doctype struts-config public "-//apache software foundation//dtd struts configuration 1.1//en"
" http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd ">
<struts-config>
<!--formbean是struts的一個概念,本質是javabean,用來自動存儲頁面表單中各個域的值,并在適當的時候回填表單域,不需要象傳統那樣request.getparameter(“fieldname”);,常被action-mappings中的action 使用-->
<form-beans>
<!—稍后我們會新增一個userform類,用來存儲用戶信息。-->
<form-bean name="userform" type="test.userform"/>
</form-beans>
<!--這里存放整個系統都可以使用的全局轉向中轉(forward)地址,類似于javascript中的window.location(‘index.jsp');,也類似于電視控制器上的各種按鈕,可以轉頻道、調色等等是基于struts的web應用的控制流程流轉。一般情況下,一個action處理完畢后,會轉發到一個jsp頁面進行顯示。這也是jsp中的mvc的實現的要點。-->
<global-forwards>
<!--failed.cool將被當成servlet請求,到action-mappings中尋找對應的action處理。-->
<forward name="failed" path="/failed.cool"/>
<forward name="regist" path="/regist.jsp"/>
</global-forwards>
<!--還記得web.xml中后綴為cool的請求嗎?它們是轉到這里處理的。這里相當于struts的model部分,model部分是struts中比較靈活的地方。-->
<action-mappings>
<!--處理regist.cools的請求,使用的formbean是userform,既test.userform類,當處理過程發生錯誤時將返回index.jsp-->
<action path="/regist" type="test.registaction" name="userform" scope="request" input="/index.jsp" />
<action path="/overview" forward="/hello.jsp"/>
<action path="/failed" forward="/wuwu.jsp" />
</action-mappings>
</struts-config>
三、增加一個formbean,類路徑為test.userform,以下是這個類的內容:
package test;
import org.apache.struts.action.actionform;
public class userform extends actionform
{
private string name="lpw";//用戶名
private string ps="1111";//密碼
public userform(){}
public void setname(string s) {name=s;}
public string getname() {return name;}
public void setps(string s) {ps=s;}
public string getps() {return ps;}
}
四、增加一個action的子類,類路徑為test. registaction,以下是這個類的內容:
package test;
import java.lang.reflect.invocationtargetexception;
import java.util.locale;
import javax.servlet.servletexception;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpsession;
import javax.servlet.http.httpservletresponse;
import org.apache.struts.action.action;
import org.apache.struts.action.actionerror;
import org.apache.struts.action.actionerrors;
import org.apache.struts.action.actionform;
import org.apache.struts.action.actionforward;
import org.apache.struts.action.actionmapping;
import org.apache.struts.util.messageresources;
import test.userform;
public final class registaction extends action
{
public actionforward execute(actionmapping mapping,actionform form, httpservletrequest request, httpservletresponse response)
throws exception
{
locale locale = getlocale(request);
messageresources messages = getresources(request);
httpsession session = request.getsession();
userform userform = (userform) form;
//此處可以調用其他類來執行數據庫寫入或其他邏輯判斷
// 如果userform傳來的參數name的值為默認的lpw,將forward到failed,
// 該名稱將到struts-config.xml的<global-forwards>中尋找映射的url地址
// (可以是絕對路徑,也可以是相對路徑),對于本例,是轉到failed.cool,
// 還記得嗎?后綴為cool的請求全部到action-mappings中尋找
// 對應的action處理,最終目錄是wuwu.jsp*/
if( "lpw".equals(userform.getname()) )
return (mapping.findforward("failed"));
else
return (mapping.findforward("regist"));
}
}
五、以下所有新增或修改的頁面相當于struts的view部分,把首頁index.jsp改成:
<%@ page contenttype="text/html;charset=gbk" language="java" %>
<%@ page import = "test.*" %>
<a href="overview.cool">站點導航</a><br>
<form action="regist.cool" method="post">
<!—表單中的域的名稱要和userform中的參數一樣,就可以實現數據自動獲取功能,不需要用request.getparameter(“param”);-->
用戶:<input type="text" name="name"><br>
密碼:<input type="password" name="ps"><br>
<input type=submit value="新增用戶">
</form>
六、增加hello.jsp,用于站點導航:
<h1>site map</h1>the following is content filling by reader
七、增加wuwu.jsp,當沒有新用戶登陸時,將轉到這個頁面:
<%@ page contenttype="text/html;charset=gbk" language="java" %>
<jsp:usebean id="beanlpw" class="test.userform" scope="session"/>
現有用戶:<%=beanlpw.getname()%><br>
密碼:<%=beanlpw.getps()%><br>
沒有得到新的用戶!
八、增加regist.jsp,當有新用戶登陸時,將轉到這個頁面:
<%@ page contenttype="text/html;charset=gbk" language="java" %>
<jsp:usebean id="beanlpw" class="test.userform" scope="session"/>
新用戶帳號:<%=beanlpw.getname()%><br>
密碼:<%=beanlpw.getps()%>
九、啟動tomcat4,瀏覽器中鍵入 http://localhost:8080/test/index.jsp ,操作一下,就可以看到結果,并初步理解struts的m、v、c各部分的協同工作原理,當然這是作者的良好意愿,如果讀者看得一頭霧水,歡迎指出錯誤在哪里 :
新聞熱點
疑難解答