Ho il seguente struts-config.xml:
mentre la classe LoginAction è la seguente:codice:<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="loginForm" type="com.storefront.struts.form.LoginForm"/> </form-beans> <global-exceptions> <exception key="global.error.invalidlogin" path="/login.jsp" scope="request" type="com.storefront.struts.exception.InvalidLoginException"/> </global-exceptions> <global-forwards> </global-forwards> <action-mappings> <action input="/login.jsp" name="loginForm" scope="request" validate="true" path="/login" type="org.apache.struts.action.LoginAction"> <forward name="success" path="/action/getMainPage" redirect="true"/> <forward name="failure" path="/login.jsp" redirect="true"/> </action> </action-mappings> <controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/> <message-resources parameter="com/myapp/struts/ApplicationResource"/> <plug-in className="org.apache.struts.tiles.TilesPlugin" > <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" /> <set-property property="moduleAware" value="true" /> </plug-in> <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/> </plug-in> </struts-config>
la classe SecurityService è invece:codice:package com.storefront.struts.action; import com.storefront.struts.form.LoginForm; import com.storefront.struts.service.IAuthentication; import com.storefront.struts.service.SecurityService; import com.storefront.struts.view.UserView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /** * * @author campagna */ public class LoginAction extends org.apache.struts.action.Action { /* forward name="success" path="" */ private static final String SUCCESS = "success"; @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward forward=null; UserView userView=null; String email=((LoginForm)form).getEmail(); String password=((LoginForm)form).getPassword(); IAuthentication service=new SecurityService(); userView=service.login(email, password); HttpSession session=request.getSession(false); if(session!=null){ session.invalidate(); } session=request.getSession(true); session.setAttribute("USER_VIEW_KEY", userView); return mapping.findForward(SUCCESS); } }
se inserisco nei campi email e password dei valori diversi da "123" e "456" ottengo una eccezione che non viene gestita mentre nel file struts-config.xml è inserito l'eccezione InvalidLoginException......codice:package com.storefront.struts.service; import com.storefront.struts.view.UserView; import com.storefront.struts.exception.InvalidLoginException; /** * * @author campagna */ public class SecurityService implements IAuthentication{ public UserView login(String userName,String pwd) throws InvalidLoginException{ if("123".equals(userName) && "456".equals(pwd)){ UserView userView=new UserView("John","Doe"); userView.setId("39017"); return userView; }else{ String msg="Invalid Login Attempt by"+userName+":"+pwd; System.out.print(msg); throw new InvalidLoginException(msg); } } }
perchè?
tulipan

Rispondi quotando