Home > Back-end >  Unable to find forward in struts
Unable to find forward in struts

Time:07-19

FormBackup.java

package beans;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class FormBackup extends ActionForm{

    private String name;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) {
        ActionErrors ae = new ActionErrors();
        if(name.equals(""))
            ae.add("name",new ActionMessage("msg"));
        return ae; 
    }
}

FormController.java

package beans;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class FormController extends Action{

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        
        String name=request.getParameter("name");
        request.setAttribute("res", "Hello..."  name);
        return mapping.findForward("success");  
    }
}

index.jsp

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<h1>Validation Form</h1>

<html:form action="Hello">
    Name:<html:text property="name"/><html:errors/>
    <html:submit value="submit"/>
</html:form>

success.jsp

<%=request.getAttribute("res")%>

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<struts-config>

    <form-beans>
        <form-bean name="HF" type="beans.FormBackup" />
    </form-beans>
    <action-mappings>
        <action path="/Hello" name="HF" input="/index.jsp"
            type="beans.FormController"></action>
        <forward name="success" path="/success.jsp" redirect="true" />
    </action-mappings>
    <message-resources parameter="beans.Messages" />
</struts-config>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee;http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>FirstStrutsExample</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>actionservlet</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>actionservlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <jsp-config>
        <taglib>
            <taglib-uri>/tags/struts-html</taglib-uri>
            <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
        </taglib>
    </jsp-config>
</web-app>

I'm new to Struts here I have created simple hello form in which index page gets executed, and then when I'm trying to submit the form it shows unable to find "success" forward. I don't know where I had the exact error. can someone can help me with this. when I hit the submit button it shows me like

"org.apache.struts.action.ActionMapping findForward WARNING: Unable to find 'success' forward."

CodePudding user response:

In the configuration struts-config.xml you didn't added <forward> to the <action> Tag.

<action path="/Hello" name="HF" input="/index.jsp" type ="beans.FormController">
<forward name ="success" path ="/success.jsp"/>
</action>

There should be XML parsing errors at sturtup because this document doesn't comply struts-config.DTD.

In the struts-config.xml you should include a correct doctype against which DTD is validated.

<!DOCTYPE struts-config PUBLIC     
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "struts-config_1_3.dtd">

  • Related