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>
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
i think you need to make two changes in the code
public class HelloWorldAction extends Action
is the 1st one and 2nd, user struts property for form to post action
<s:form action="helloAction">
Hope will help you.
你应该在你的动作类中扩展Action
:
public class HelloWorldAction extends Action {
Try by extending com.opensymphony.xwork2.ActionSupport
class and override execute method like this
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport{
public String execute() {
System.out.println(userName);
return "success";
}
}
beware: it is for Struts 1
I had the same problem, but I've solved mine by removing forward
property in action-mappings
This one is correct:
<action-mappings>
<action input="/user_list.jsp" name="UserAddFormBean" path="/userAdd"
scope="request"
type="com.minetronics.struts.UserAdd" validate="true" forward="/user_add.jsp">
<forward name="success" path="/user_add.jsp"/>
</action>
</action-mappings>
But this is going to skip calling execute
and will go straight to forward
<action-mappings>
<action input="/user_list.jsp" name="UserAddFormBean" path="/userAdd"
scope="request"
type="com.minetronics.struts.UserAdd" validate="true">
<forward name="success" path="/user_add.jsp"/>
</action>
</action-mappings>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.