问答详情
源自:4-10 关于input的代码演示-深入Struts2

提示不能为空的功能实现不了?

http://img.mukewang.com/578f8adf0001cce407490191.jpg

不填写内容,直接提交,出现下面的错误提示:

警告: 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","用户名不能为空");
		}
	}


提问者:LebronJames_ 2016-07-20 22:35

个回答

  • 二度
    2016-07-28 11:03:20
    已采纳

    <action name="loginAction" method="login" class="loginDemo.loginAction">

                <result name="input">/success.jsp </result>

            </action>


  • 柚子KiYou丶
    2016-07-31 20:52:29

    楼上正解,result的name默认类型是success。

  • LebronJames_
    2016-07-20 23:43:24

    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>


  • 柚子KiYou丶
    2016-07-20 23:13:51

    action和result的xml贴出来看看