(1) 在struts-config.xml中加入插件
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
</plug-in>
loginForm.password=password
error.usevalidate=username not found
errors.required={0} is required.
errors.minlength={0} can not be less than {1} characters.
errors.maxlength={0} can not be greater than {1} characters.
errors.invalid={0} is invalid.
errors.byte={0} must be a byte.
errors.short={0} must be a short.
errors.integer={0} must be an integer.
errors.long={0} must be a long.
errors.float={0} must be a float.
errors.double={0} must be a double.
errors.date={0} is not a date.
errors.range={0} is not in the range {1} through {2}.
errors.creditcard={0} is an invalid credit card number.
errors.email={0} is an invalid e-mail address.
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.struts.action.ActionMapping;
public class MyInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation arg0) throws Throwable {
HttpServletRequest request=null;
ActionMapping mapping =null;
Object[] kk=arg0.getArguments();
for(int i=0;i<kk.length;i++){
if(kk[i] instanceof HttpServletRequest) request=(HttpServletRequest)kk[i];
if(kk[i] instanceof ActionMapping ) mapping=(ActionMapping) kk[i];
}
String path=mapping.getPath();
HttpSession session=request.getSession();
if(session.getAttribute("user")!=null)
{
return arg0.proceed();
}
else{
return mapping.findForward("login");
}
}
}
<property name="beanNames">
<list>
<value>/login</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>myintercepter</value>
</list>
</property>
</bean>
<bean id="myintercepter" class="com.test.interceptor.MyInterceptor"></bean>