一、利用struts標籤庫
1、導入標籤庫:<%@ taglib prefix="s" uri="/struts-tags" %>
2、主要爲from表單:
<s:form action="fromAct" method="post" namespace="/from">
<s:textfield name="name" id="name" label="姓名"></s:textfield>
<s:password name="password" id="password" label="密碼" tooltip="請輸入密碼" maxlength="15"></s:password>
<s:textfield name="age" id="age" label="年齡"></s:textfield>
<s:select list="{'成都','昆明' }" name="address.province" headerKey="0" headerValue="請選擇省" lable="省"></s:select><br/>
<s:select list="{'雙流','呈貢' }" name="address.city" headerKey="0" headerValue="請選擇市" lable="市"></s:select>
<s:submit value="註冊"></s:submit>
</s:form>
建議:表單內的name屬性最好和Action類的成員變量相同
可參考:http://blog.163.com/hzd_love/blog/static/131999881201082111852520/
二、struts在xml中的代碼提示解決方法(我用的2.1.8版本)
1.首先在struts中搜索struts-2.0.dtd文件
2.在設置preferences——Files and Editors——xml——xml Catalog中設置,location中填 入: struts-2.0.dtd文件的路徑,keytype中選擇uri,key 填http://struts.apache.org/dtds/struts- 2.0.dtd(從struts.xml複製的)
三、路徑的訪問順序
1、先找自己的命名的命名空間,找不到再默認空間
2、namespace="/"和默認是一樣的效果
3、http://localhost:8080/Struts2/test/asdff/sasdfd/HelloWorld的訪問的順序是先在sasdfd 中HelloWorld這個Action,然後再到asdff中找,之後再找test中,這就是爲什麼只有test中有 HelloWorld的,但是輸入面的地址仍然可以訪問HelloWorld的原因。如果都找不到就只有到默 認的命名空間找。
四、包含配置:主要是爲了不要將大篇幅的配置信息都寫得struts.xml中和適應多人共同開發
<struts>
<include file="struts-test.xml" />
<include file="struts-from.xml" />
<include file="struts-chain.xml" />
<include file="struts-dispacher.xml" />
<include file="struts-redirectT.xml" />
<include file="struts-plainText.xml" />
<include file="struts-global.xml" />
</struts>
五、開發模式:
開發中配置文件的更改,在訪問時讓框架自動重新加載:
struts.devMode = false(default.properties)
利用strutx.xml中的constant元素來覆蓋掉default.properties默認行爲,這樣就避免了重複的發佈問題,例如下:
<struts>
<constant name="struts.devMode" value="true"></constant>
</struts>