First project & question

照着書《STRUTS 開發入門與項目實踐》的光盤裏的Chapter2的S01_Begin  做了一下,其實是copy, debug。界面是可以跳轉了,但還是有問題,日誌還是寫不了的。現在分析一下代碼:

login.jsp:

<%@ page contentType="text/html;charset=GBK" language="java" %>
<body vLink="#006666" link="#003366" bgColor="#E0F0F8">
<img height="33" src="enter.gif" width="148">
<form action="login.do" method="post">
用戶名: <input size="15" name="name"><p>
密碼: <input type="password" size="15" name="psw"><p>
<input type="submit" value="登錄">
</form> 

其中紅色部分會根據struts-config.xml裏面的配置內容來調用相應的Action。

<form-beans>
 <form-bean name="formBean1" type="classmate.UserForm"/>
  </form-beans>
  <global-exceptions />
  <global-forwards>
  <forward name="failed" path="/error.jsp"/>
  <forward name="successed" path="/right.jsp"/>
  </global-forwards>
  <action-mappings>
  <action path="/login" type="classmate.LoginAction" name="formBean1" scope="request"  />
  <action path="/regist" forward="/regist.jsp"/>
  </action-mappings>

其中:type="classmate.LoginAction" name="formBean1" 再根據藍色部分來找到相應的ActionForm(這裏是UserForm)。 ActionForm就是從表單裏提取信息。

有個問題就是:若表單是下面的形式從瀏覽器裏怎麼看不到上邊的<html:text../> & <html:submit>,真是奇怪,剛開始看,現在對這些STRUTS和HTML標籤也不瞭解。下面的對話from:  http://www.mysdn.cn/Java/Webkaifa/20061102/58854.html

問題解決:要在這個JSP文件頭加上:<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

structs中是不是表單必須用<html:form>??



如果你用的普通form ,form裏面的表單元素卻用struts 標籤的話,是會報錯的,除非都改爲html標籤,不過你用struts form 裏面是可以用html表單元素的


<p><html:form action="/register.do">
用戶名:<html:text property="username"/><br/>
密碼1: <html:text property="password1"/><br/>
密碼2:<html:text property="password2"/><br/>

<html:submit value="Register"/>
</html:form></b>
<form action="/register.do">
<input type="text" name="username" value="" /><br/>
<input type="text" name="password1" value="" /><br/>
<input type="text" name="password2" value="" /><br/>
<input type="submit" value="ok" />
</form>
//////////
點第一個按鈕,沒問題,但是第二個按鈕提示:

type Status report

message /register.do

description The requested resource (/register.do) is not available.




Form的action,如果用struts標籤的話"/"表示當前web應用的根目錄,如果用html標記的話"/"表示localhost的根目錄。第二種把/register.do改成register.do試試。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章