Struts2中No result defined for action XXXAction and result input问题的解决

问题背景:
在一个JSP页面中需要动态跳转到另外一个Action(不是当前页Form的Action),需要向这个Action传递一个当前Form中没有的属性值,因此在jsp而面中追加了类似如下的属性:

<input type="hidden" id="driverid" name="driverid" value="" />
<input type="hidden" id="drivername" name="drivername" value="" />


结果目标Action怎么都无法进入,根本就运行不到Action中去,页面抛出以下异常信息:

Struts Problem Report

Struts has detected an unhandled exception:
Messages:


No result defined for action com.kotei.smbcloud.web.action.statisticevaluate.StatisticEvaluateAction and result input
File: file:/E:/workspace_odc5/.metadata/.plugins/org.eclipse.wst.server.core/tmp2/wtpwebapps/SMBCloud/WEB-INF/classes/struts.xml
Line number: 194
Column number: 84
         <!-- Station Manage end-->
          <!-- ????? -->
<action name="statistic_evaluate_*" class="statisticEvaluateAction" method="{1}">
<result name="toInitPage">/jsp/statisticEvaluate/evaluateMgr.jsp</result>
<result name="showEvaluate">/jsp/statisticEvaluate/showStatisticEvaluate.jsp</result>
Stacktraces

No result defined for action com.kotei.smbcloud.web.action.statisticevaluate.StatisticEvaluateAction and result input - action - file:/E:/workspace_odc5/.metadata/.plugins/org.eclipse.wst.server.core/tmp2/wtpwebapps/SMBCloud/WEB-INF/classes/struts.xml:194:84
    com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:371)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:273)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:76)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)



原因:
纠结来纠结去,还查了半天,最终自己摸清楚原因了。原来是自己在jsp页面中追加了相关的隐藏属性,但当前页Form中对应的Action中没有定义这个属性,因此Struts2在封装页面参数到当前Action时找不到对应的属性而发生错误。

解决办法:
有两种办法,选其一:
1、在当前页Form对应的Action中也追加相应属性。
2、不使用隐藏Input字段,而使用javaScript动态为Form增加属性,提交表单。

例如:
在jsp中使用相应的link配合js提交表单:
<a class="linkToShowUserStar" href="#" attrdriverid="${item.driverinfo.id }" attrdrivername='${item.driverinfo.username }' attrintStar="5" attryear="${item.year}" attrmonth="${item.month}">${item.evaluate5 }</a>


js动态追加表单属性提交表单(JQuery):
<script type="text/javascript">
$(function(){
	$('.linkToShowUserStar').click(function(){
		action = "showEvaluateDetail_showUserDetail.action";			
		//alert($(this).attr("attrdriverid"));
		//alert($(this).attr("attrdrivername"));
		//alert($(this).attr("attrintStar"));
		//alert("strOldCon:" + $('#strMonth').val() +  "_" + $('#selectError').val());
		
		form = $("<form></form>");
		form.attr('action', action);
		form.attr('method', 'post');
		input1 = $("<input type='hidden' name='driverid' />");
		input1.attr('value', $(this).attr("attrdriverid"));
		form.append(input1);
		
		input2 = $("<input type='hidden' name='drivername' />");
		input2.attr('value', $(this).attr("attrdrivername"));
		form.append(input2);
		
		input3 = $("<input type='hidden' name='intStar' />");
		input3.attr('value', $(this).attr("attrintStar"));
		form.append(input3);
		
		input4 = $("<input type='hidden' name='strOldCon' />");
		input4.attr('value', $('#strMonth').val() +  "_" + $('#selectError').val());
		form.append(input4);
		
		input5 = $("<input type='hidden' name='strMonth' />");
		input5.attr('value', $(this).attr("attryear") + "-" + $(this).attr("attrmonth"));
		form.append(input5);
		
		form.appendTo("body");
		form.css('display', 'none');
		form.submit();
		return false;		
	});
});

</script>

你可能感兴趣的:(JavaScript,jsp,struts)