1. 程式人生 > >Spring、Struts2整合,velidate()驗證失敗返回input不進入execute()

Spring、Struts2整合,velidate()驗證失敗返回input不進入execute()

在整合ssh的專案中,ss整合跳轉action出現了問題,from表單提交action,進入velidate()驗證返回一個input,不進入execute(),

一直檢查配置檔案和專案原始碼,但始終找不到源頭所在,後來經過大神的一番原始碼解析後,終於找到了問題所在,下面上程式碼

 

 form表單的程式碼:

<s:form action="loginUser" method="POST" namespace="/page">
	<div class="infos">
	
		<table class="field">					

			<tr>
				<td colspan="2" ><s:fielderror/> ${message}</td>
			</tr>
			<tr>
				<td class="field">用 戶 名:</td>
				<td><!-- <input type="text" class="text" name="name" /> -->
					<s:textfield name="user.name" cssClass="text" required="true"/>
				</td>
			</tr>
			<tr>
				<td class="field">密  碼:</td>
				<td><!-- <input type="password" class="text" name="password" /> -->
				<s:password name="user.password" cssClass="text" required="true"/>
			</td>
			</tr>
		</table>
		<div class="buttons">
			<s:submit value="立即登入" />
			<input type='button' value='註冊' onclick='document.location="page/register.jsp"'/>
		</div>
	</div>
</s:form>

struts2 xml:

<struts>
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <constant name="struts.ui.theme" value="simple"/>
    <constant name="struts.objectFactory.spring.autoWire" value="type"/>
    <package name="page" namespace="/page" extends="struts-default" >
        <action name="loginUser" class="landun.qiqi.action.LoginAction" >
            <!--登入成功後跳轉到houseList Action -->
            <result name="success">/page/house/house_list.jsp</result>
            <result name="login">/page/login_struts2.jsp</result>
            <result name="input">fail.jsp</result>
        </action>
    </package>
    <include file="struts-user.xml"/>
</struts>

action:

    public void validate(){
        System.out.println("validateLogin^");
    }
    public String execute() {
        System.out.println("execute");
    }

執行結果:

 

除錯發現的錯誤:

 

 

 除錯了很久原始碼查了好多資料都沒有發現問題所在,後來大神檢查了一波struts.xml,發現是一條程式碼導致的:

原本是想設定按type來自動注入,沒想到最後導致了一個蜜汁bug,這條程式碼具體是什麼原因導致出現問題我還沒弄明白;

我個人感覺是因為專案中的action是以set方式注入的bean,而這條程式碼設定為type,導致某個原因注入不了出現問題,以後深入

瞭解後再來補充!