分享

Struts 2 – No result defined for action and result input

 邵飞翔 2016-07-08

Recently while working on a Struts 2 project, I got a strange error that took me some time to solve. My project had a simple JSP page like below.

<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www./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:

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.

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

<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  "http://struts./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.

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

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

And in server 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)

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.

<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. 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.

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多