不填写内容,直接提交,出现下面的错误提示:
警告: Could not find action or result
No result defined for action loginDemo.loginAction and result input
at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:375)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:277)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
.....
login.jsp部分代码
%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> ...... <body> <form action="loginAction.action" method="post"> 用户名: <input type="text" name="username"><s:fielderror name="username"></s:fielderror> 密码: <input type="password" name="password"> 书籍1: <input type="text" name="bookList[0].username"> 书籍2: <input type="text" name="bookList[1].username"> <input type="submit" value="提交"/> </form> </body>
web.xml部分代码
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
loginAction 部分代码
public void validate() { if(user.getUsername() == null ||"".equals(user.getUsername())){ this.addFieldError("username","用户名不能为空"); } }
<action name="loginAction" method="login" class="loginDemo.loginAction">
<result name="input">/success.jsp </result>
</action>
楼上正解,result的name默认类型是success。
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <action name="loginAction" method="login" class="loginDemo.loginAction"> <result >/success.jsp </result> </action> </package> </struts>
action和result的xml贴出来看看