NoNaMe NoNaMe - 10 days ago 4
Java Question

Struts 2 not calling action class execute() method

i am new to Struts2 and created a simple HelloWorld app in struts but the issue is my action class is not being called when i click the submit button, there is not any exception on the console as well.
here is my code,

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 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>


struts.xml


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

<struts>
<constant name="struts.enable.DynamicMethodInvocation"
value="false" />
<constant name="struts.devMode" value="true" />

<package name="default" extends="struts-default" namespace="/">
<action name="helloAction"
class="com.tutorial.struts2.HelloWorldAction">
<result name="success">helloworld.jsp</result>
</action>
</package>
</struts>


index.jsp


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Welcome to Struts</h1>
<form action="/helloAction">
<label for="name">Please enter your name</label><br/>
<input type="text" name="userName"/>
<input type="submit" value="Say Hello"/>
</form>
</body>
</html>


HelloWorldAction


package com.tutorial.struts2;


public class HelloWorldAction {

public String userName;

public String execute() throws Exception{
System.out.println(userName);
return "success";
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}
}


helloworld.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">
<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Hello World, <s:property value="userName"/>

</body>
</html>

Answer

helloworld.jsp

<s:property value="name"/>

where is name property?? in which action class you have declared name property?

It should be <s:property value="userName"/>

remember struts will try to find out the getter method of your property file by putting the get+YourProperty()

In your case it will, try to find out getName() method inside your action class which is not available.

Edited:

Your url for helloAction is not mapped correctly try to run this in your browser,

http://yourIp:port/yourApplicationName/yourNameSpace/yourAction

which will become for your project as

http://yourIp:8080/HelloWorldStruts/testNameSp/helloAction
Comments