-
這個方法我只是針對通配符使用的
我看到視頻里面兩個方法好像都是return SUCCESS;所以改了url地址一直都無法跳轉到其他兩個頁面,如果有和我一樣的情況的同學可以把success改為"add"和"update",這樣就沒問題了
查看全部 -
Struts官方網站:http://struts.apache.org/
歷史版本:http://archive.apache.org/dist/struts/
查看全部 -
搭建Struts環境步驟:
查看全部 -
Struts2環境:
查看全部 -
Struts2
查看全部 -
數據類型轉換錯誤時會返回INPUT
(1)校驗方法在LoginAction中添加validate()
public?void?validate(){ ????if(user.getUsername()==null||"",equals(user.getUsername)){ ????????this.addFieldError("username","用戶名不可為空!"); ????} }
(2)表單頁面加入struts2標簽
<%@?taglib?prefix="s"?uri="/struts-tags"?%> <input?type="text"?name="username"?/><s:fielderror?name="username"></s:fielderror>
查看全部 -
action接收參數
有表單如下:
<form?action="LoginAction"?method> ????<input?type="text"?name="username"?/> ????<input?type="password"?name="password"?/> ????<input?type="submit"?value="提交"?/> </form>
創建LoginAction,添加login()
public?String?login(){ ????//******** ????return?SUCCESS; }
1、屬性接收
//在LoginAction中建立username、password屬性 private?String?username; private?String?password; //實現其get、set方法 //login()可以調用username、password
注:屬性接收簡單,但對于屬性多的情況不利于代碼維護
2、DomainModel接收
????(1)創建user類
public?class?User{ ????private?String?username; ????private?String?password; ????//實現其get、set方法 }
????(2)LoginAction中聲明User
private?User?user; //實現User的get、set方法
注:在form表單中,input的name要指定類名如user.username.
3、實現ModelDriven接口
LoginAction實現ModelDriven接口
public?class??LoginAction?extends?ActionSupport?implements?ModelDriven<User>{ ????//用ModelDriven必須實例化User ????private?User?user=new?User(); ????public?String?login(){ ????????return?SUCCESS; ????} ????public?User?getModel(){ ????????return?user; ????}
4、復雜參數接收
(1)user類包含ListBook類
public?class?User{ ????private?String?username; ????private?String?password; ????private?List<ListBook>?listbook; ????//實現其get、set方法 }
(2)新建ListBook類
public?class?ListBook{ ????private?String?bookname; ????private?int?price; ????//實現其get、set方法 }
(3)form表單中添加
<input?type="text"?name="listbook[0].bookname"> <input?type="text"?name="listbook[0].price"> <input?type="text"?name="listbook[1].bookname"> <input?type="text"?name="listbook[1].price">
(4)login()中獲取
String?book1=user.getListBook().gte(0).getBookName(); String?book2=user.getListBook().gte(1).getBookName();
查看全部 -
1、多個配置文件可以用<include>,在struts.xml中統一配置
<include?file="xx.xml"></include>
2、設置常量(此方法可以頂替struts.properties文件)
<constant?name="常量名"?value="常量值"></constant>
3、動態方法調用+通配符使用
<action?name="*_*"?method="{2}"?class="包全名.{1}"> ????<!--?*_*中*代表任意字符,_代表固定格式 ????????{1}{2}是第幾個*所代表的字符串 ????????例:hello_add ????????{1}=hello【action名】;{2}=add【action中方法名】 ????????調用hello.acton中的add() ????--> ????<result>/xx.jsp</result> </action>
4、默認action(用來處理找不到相應action時,統一跳到一個友好界面)
<default-action-ref?name="aaa"></default-action-ref> <action?name="aaa"> ????<result>/xx.jsp</result> </action>
注:當同包中存在action name="*_*"時,訪問路徑寫成hello_aaa不能跳到指定的目標。用為會默認先匹配action部分,匹配成功后就不再找默認action了。
5、struts2后綴
<!--?struts.xml中添加常量修改action后綴?--> <constant?name="struts.action.extension"?value="html"></constant> <!--?1、后綴再寫action報錯,要改成.html? ?????2、該常量也可以在struts.properties中配置 ?????????struts.action.extension=action,do,struts2 ?????3、也可以在過濾器<filter>中配置 ?????????<init-param> ?????????????<param-name>struts.action.extension</param-name> ?????????????<param-value>do</param-value> ?????????</init-param> -->
查看全部 -
Struts2是基于MVC設計模式的Web應用程序框架,能節省Web應用開發時間
Struts實例程序創建步驟(工具Myeclipse10+tomcat7+jdk1.7):
1、創建Web Project
2、導入jar包,在src下建立struts.xml
3、打開web.xml,添加
<filter> ????<filter-name>[過濾器名]</filter-name> ????<filter-class>??? ????org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter ????</filter-class> </filter>
<filter-mapping> ????<filter-name>[過濾器名]</filter-name> ????<url-pattern>/*</url-pattern> ????<!--?/*代表過濾所有路徑?--> </filter-mapping>
4、打開struts.xml,假如文檔類型定義DTD(用myeclipse直接添加struts后不用會搜懂添加DTD)
5、創建action,新建class(命名hello)繼承ActionSupport類,默認執行ActionSupport的execute() 執行成功返回SUCCESS.
6、在struts.xml中配置action
<package?name="default"?namespace="/"?extends="struts-default"> <!--?package?name="default"???是默認包名,可以自定義,用于被其他包繼承 ????????namespace="/"????命名空間,可以指定可以默認 ???????extends="default"????該包所繼承的包 --> ????<action?name="hello"?method=""?class="包全名.hello"> ????????<result?name="">/result.jsp</result> ????????<!--?result?的name默認是SUCCESS?也可以是其他action中傳回的字符串?--> ????</action> </package>
7、在WebRoot下創建result.jsp
8、發布運行項目,打開瀏覽器訪問http://localhost:8080/[項目名]/hello.action。
注:myeclipse最高支持jdk1.7+tomcat7.x
本機最初配置是jdk1.8,運行項目后報錯如下
????The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files
查看全部 -
返回 結果類型 中的 type
查看全部 -
處理結果類型? result 的type?
查看全部 -
處理結果類型中 :
局部結果 <action> 中套入 <result>
全局結果 <global-result>?中套入 <result>
查看全部 -
在struts2?的結果類型中?
使用?result標簽?若不填name屬性? ?則采用默認值success
查看全部 -
struts2?返回結果是字符串 ,
return "success"? ??
找到對應視圖
查看全部 -
struts2?調用方式
查看全部
舉報