今天第一次感覺到經濟危機在我身邊了,部門現在沒有在做的項目了
經濟危機中,趕緊為自己充充電,好到時候柳暗花明又一村,哈哈
學struts2
據說struts2基于webwork,基本上跟struts1沒啥關系,如果有webwork的經驗上手會很迅速
我沒接觸過webwork,就知道有這么個東西
今兒開始第一個struts
見過好多blog寫有struts2的入門步驟,俺也寫一個,為自己造個輪子,加深印象。
首先下載struts2的jar包,到http://struts.apache.org/,右上角有個struts2的鏈接,今天下到的是
struts2的2.0.14,昨天在javaeye上看到發布
Struts2.1.3
發布了,只是主頁還沒看到,不止一次聽大拿們說過不要追求新版本,哈哈
下載后的目錄:app--struts的例子
docs-doc文檔
lib-struts的jar包或依賴包
src-源碼
HelloWorld:
1、index.jsp
耳目一新的是,不需要用到struts html標簽,這只是猜測,或許例子過于簡單?今天工作中還感覺struts1的html標簽真是不好用,想加個class、maxlength、size都不好使,讓我很是郁悶。希望在繼續學習中真的能耳目一新。
struts的action慣例后綴名改成了.action,不再像struts1的.do了,說是延續到webwork的慣例。
下面的頁面代碼submit的時候將提交到login.action
index.jsp
<body>
<form action="login.action" method="post">
username:
<input type="text" name="username"/>
password:
<input type="password" name="password"/>
<input type="submit" value="submit"/>
</form>
</body>
2、Action類
struts2的Action可是大進步,不用再繼承任何類,實現了松耦合,它好像將struts1的ActionForm融合了進來,據說struts2不再用ActionForm,頁面上對應的字段寫在了Action中,struts2框架會自動調用get/set方法,在我印象里struts1中的Action對象不是線程安全的,會在不同線程間重用,所以謹慎在里面定義字段,在這看來struts2的Action不是這樣的,只是猜測
package com.mystart.action;
public class LoginAction {
private String
username;
private String
password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.
username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.
password = password;
}
public String execute()
throws Exception{
return "success";
}
}
3、jsp、java類都定義了,現在要它們聯系起來-struts配置文件
新的struts配置文件有個package包的概念,還沒鬧明白這個package的詳細用法,有待繼續深入
<action>標簽有變化,type變成了class,path變成了name,struts1中name屬性是制定ActionForm的,現在ActionForm沒有了
forward變成了result,result的默認name=success
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts" extends="struts-default">
<action name="login" class="com.mystart.action.LoginAction">
<result name="success">/result.jsp
</result>
</action>
</package>
</struts>
4、最后啟動struts2,配置web.xml
struts1的web.xml配置是放在<servlet>中,也就是是一個servlet
struts2變成了一個過濾器Filter
struts1中<url-pattern>被配置成攔截.do的鏈接
struts2變成了攔截所有鏈接 /*
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
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">
<filter>
<filter-name>struts2
</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2
</filter-name>
<url-pattern>/*
</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
以上是驢了個struts的視頻教程,今兒看了第一節課,看完后做了一下總結,里面有一些自己的理解,有不對的地方請兄弟們指正,別光光說我是豬,豬也要進步啊,嘿嘿,每一步都有疑問,明天帶著問題接著看下一節,睡覺去。