1

The action reg1 is not called when I click submit button.

My simple struts application is as follows:

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"    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee z     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Struts2</display-name>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

reg.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"         "http://www.w3.org/TR/html4/loose.dtd">
 <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="reg1" >
    Username: <input type="text" name="username"> Password: <input
        type="text" name="password"> Mobile: <input type="text"
        name="mobile"> <input type="submit" >
</form>
</body>
</html>

struts.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default" namespace="/">
    <action name="reg1.action" class="bean.regbean">
        <result name="success">/login.jsp</result>
    </action>
</package>
</struts>
Roman C
  • 49,761
  • 33
  • 66
  • 176
hem
  • 11
  • 1

1 Answers1

2

The action is not invoked because it's incorrectly mapped to the url in the form action attribute. Use this action configuration to map the action name, which is used without .action suffix.

<action name="reg1" class="bean.regbean">
    <result name="success">/login.jsp</result>
</action>

Also note, that FilterDispatcher is deprecated in the latest Struts2 release. So, you have to upgrade and modify web.xml accordingly. In the JSP you can use struts tags to bind fields to the bean properties.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • `actionPackages` isn't necessary if you're using XML configuration, which the OP seems to be. – Dave Newton Aug 10 '14 at 17:21
  • @DaveNewton Ok, why this init parameter then? – Roman C Aug 10 '14 at 18:35
  • I assume you're talking about the old `actionPackages` config param for the old CodeBehind plugin, e.g., http://struts.apache.org/release/2.0.x/docs/zero-configuration.html? – Dave Newton Aug 10 '14 at 18:54
  • If this parameter related to CodeBehind plugin, why it's in the list of *key initialization parameters* of struts2 filter. Every code where this parameter is used is deprecated. – Roman C Aug 10 '14 at 19:03
  • Oh, so you're *not* referring to that. Basic point remains: when using XML configuration it's obviously not necessary to specify any packages to scan since they're action classes are explicitly declared in the XML configuration file. – Dave Newton Aug 10 '14 at 19:08
  • Ok, thanks for clarification, sometimes reading docs is not enough to get realized some things previously excluded from the main point of view. – Roman C Aug 10 '14 at 19:13
  • I don't believe the current docs do an adequate job of explaining many of the config params or how to alter basic framework behavior; unfortunately there aren't many devs working on S2 actively, myself included. – Dave Newton Aug 10 '14 at 19:19
  • Yeah, the last line is confusing and completely ignoring without related example of usage ;) I thought they work very actively issuing releases every month. – Roman C Aug 10 '14 at 19:29