struts2自定义标签_Struts 2 –没有为动作和结果输入定义结果

struts2自定义标签

Recently while working on a Struts 2 project, I got a strange error message No result defined for action and result input.

最近,在处理Struts 2项目时,我收到一条奇怪的错误消息: No result defined for action and result input

Struts2 –没有为动作和结果输入定义结果 (Struts2 – No result defined for action and result input)

struts2 no result defined for action and result input

Let’s look at a simple scenario where we get the error message “No result defined for action and result input”.


让我们看一个简单的场景,在该场景中,我们将显示错误消息“没有为操作和结果输入定义结果”。

My project had a simple JSP page like below.

我的项目有一个简单的JSP页面,如下所示。

login.jsp

login.jsp

<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<%-- Using Struts2 Tags in JSP --%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Login Page</title>
</head>
<body>
<h3>Welcome User, please login below</h3>
<s:form action="login">
	<s:textfield name="name" label="User Name"></s:textfield>
	<s:textfield name="pwd" label="Password" type="password"></s:textfield>
	<s:submit value="Login"></s:submit>
</s:form>
</body>
</html>

And corresponding action class as:

并对应的动作类为:

LoginAction.java

LoginAction.java

package com.journaldev.struts2.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	@Override
	public String execute(){
		//Some complex business logic
		return SUCCESS;
	}
	
	@Override
	public void validate(){
		if("".equals(getName())){
			addFieldError("name", "UserName can't be empty");
		}
		if("".equals(getPwd())){
			addFieldError("pwd", "Password can't be empty");
		}
	}
	
	//Java Bean to hold the form parameters
		private String name;
		private String pwd;
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getPwd() {
			return pwd;
		}
		public void setPwd(String pwd) {
			this.pwd = pwd;
		}
}

My struts 2 configuration file was like below.

我的struts 2配置文件如下所示。

struts.xml

struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"https://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.convention.result.path" value="/"></constant>
<package name="user" extends="struts-default">
	<action name="home">
		<result>/login.jsp</result>
	</action>
	<action name="login" class="com.journaldev.struts2.action.LoginAction">
	<result name="SUCCESS">/welcome.jsp</result>
	<result name="ERROR">/error.jsp</result>
	</action>

</package>

</struts>

Everything looked fine and when login action will be invoked, LoginAction will take care of it. Incase of success, welcome.jsp will be sent as response or error.jsp incase of any errors.

一切看起来都很好,并且在调用登录操作时,LoginAction将进行处理。 如果成功,将在任何错误的情况下将welcome.jsp作为响应或error.jsp发送。

But whenever I tried to invoke login action without passing username or password values, I get 404 error and browser response as:

但是,每当我尝试调用登录操作而不传递用户名或密码值时,都会收到404错误和浏览器响应,如下所示:

No result defined for action com.journaldev.struts2.action.LoginAction and result input

No result defined for action com.journaldev.struts2.action.LoginAction and result input

And in server logs, I was getting following exception stack trace:

在服务器日志中,我得到以下异常堆栈跟踪:

Sep 14, 2013 11:40:25 PM com.opensymphony.xwork2.util.logging.jdk.JdkLogger error
SEVERE: Exception occurred during processing request: No result defined for action com.journaldev.struts2.action.LoginAction and result input
No result defined for action com.journaldev.struts2.action.LoginAction and result input
	at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:373)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:275)
	at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)

修复–没有为操作和结果输入定义结果 (Fix – No result defined for action and result input)

I was clueless for sometime as what is going on, then I looked at the message and it says that “no result is defined for input“. So I modified my login action like below.

一段时间以来,我一无所知,然后查看了消息,并说“ input未定义结果”。 因此,我修改了我的登录操作,如下所示。

<action name="login" class="com.journaldev.struts2.action.LoginAction">
	<result name="SUCCESS">/welcome.jsp</result>
	<result name="ERROR">/error.jsp</result>
	<result name="input">/login.jsp</result>
</action>

This simple change solved the issue and I was getting login.jsp page as response with error message set in the validate() method of LoginAction.

这个简单的更改解决了该问题,我得到了Login.jsp页面作为响应,并在LoginAction的validate()方法中设置了错误消息。

The reason is that “input” is the default result returned by Struts 2 whenever there is a problem with validating the parameters passed to an action.

原因是,当验证传递给操作的参数有问题时,“输入”是Struts 2返回的默认结果。

I hope this quick solution will save someone’s time when working with Struts 2 and using form fields validation.

我希望这种快速解决方案可以节省使用Struts 2和使用表单域验证的时间。

翻译自: https://www.journaldev.com/2164/struts-2-no-result-defined-for-action-and-result-input

struts2自定义标签

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值