1.編寫相應的異常類;
2.捕捉相應的異常類,進行拋出,可以把該代碼封裝到相應的方法中,比如在LoginDAO這個類中,有個isLogin()方法判斷是否登陸成功,同時根據需要拋出相應異常,如:UserNotFoundException、PasswordErrorException等。在LoginAction這個Action類中調用LoginDAO類中的isLogin()方面驗證用戶是否登陸成功;
3.在國際化資源文件中,編寫相應異常的key,以及對于的value值;
4.在
struts-config.xml中配置以上相關需要的信息,同時,配置<exception/>標簽,指定相應的key,type的屬性,key值對應國際化資源文件中異常的key值,type為自己編寫的異常類。同時,在相應Action中配置input屬性,該屬性為出現異常時進行跳轉的頁面。如在<exception/>中配置path屬性,則path屬性優先于<action/>中的input屬性。
5.在需要顯示錯誤提示的jsp頁面用<html:errors/>標簽進行錯誤信息讀取。
LoginDAO類:
- public class LoginDAO {
- public void isLogin(String username,String password){
- if(!(username.equals("admin"))){
- throw <SPAN style="COLOR: #ff0000">new UserNotFoundException();</SPAN>
-
- }
- else if(!(password.equals("admin"))){
- throw <SPAN style="COLOR: #ff0000">new PasswordErrorException()</SPAN>
- ;
- }
- }
- }
-
- <SPAN style="COLOR: #ff0000"><STRONG>LoginAction類:</STRONG>
- </SPAN>
-
- package wiki.struts;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.action.DynaActionForm;
-
- public class LoginAction extends Action {
-
- @Override
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- DynaActionForm daf = (DynaActionForm)form;
- String username = (String)daf.get("username");
- String password = (String)daf.get("password");
- LoginDAO loginDAO = new LoginDAO();
- loginDAO.isLogin(username, password);
- return mapping.findForward("success");
- }
- }
-
public class LoginDAO {
public void isLogin(String username,String password){
if(!(username.equals("admin"))){
throw new UserNotFoundException();
//事先聲明的異常類
}
else if(!(password.equals("admin"))){
throw new PasswordErrorException()
;//事先聲明的異常類
}
}
}
LoginAction類:
package wiki.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DynaActionForm daf = (DynaActionForm)form;
String username = (String)daf.get("username");
String password = (String)daf.get("password");
LoginDAO loginDAO = new LoginDAO();
loginDAO.isLogin(username, password);
return mapping.findForward("success");
}
}
struts-config.xml中的部分配置
- <action-mappings>
- <action path="/login" type="wiki.struts.LoginAction"
- name="loginForm" scope="request" <SPAN style="COLOR: #ff0000">input="/login.jsp"</SPAN>
- >
- <SPAN style="COLOR: #ff0000"><exception key="login.user.name.error"
- type="wiki.struts.UserNotFoundException" path="/login_error.jsp" />
- <exception key="login.user.password.error"
- type="wiki.struts.PasswordErrorException" path="/login_error.jsp" /></SPAN>
-
- <forward name="success" path="/login_success.jsp" />
- </action>
<action-mappings>
<action path="/login" type="wiki.struts.LoginAction"
name="loginForm" scope="request" input="/login.jsp"
>
<exception key="login.user.name.error"
type="wiki.struts.UserNotFoundException" path="/login_error.jsp" />
<exception key="login.user.password.error"
type="wiki.struts.PasswordErrorException" path="/login_error.jsp" />
<forward name="success" path="/login_success.jsp" />
</action>
<script type="text/javascript"><!--
google_ad_client = "pub-8638528334131919";
/* 728x90, 創建于 09-3-18 */
google_ad_slot = "1046175043";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="
</script>