java Struts 2 不调用动作类 execute() 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12928017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:49:27  来源:igfitidea点击:

Struts 2 not calling action class execute() method

javastruts2

提问by NoNaMe

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,

我是 Struts2 的新手,并在 struts 中创建了一个简单的 HelloWorld 应用程序,但问题是当我单击提交按钮时没有调用我的操作类,控制台上也没有任何异常。这是我的代码,

web.xml

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

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

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

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

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>

采纳答案by subodh

helloworld.jsp

helloworld.jsp

<s:property value="name"/>

where is nameproperty?? in which action class you have declared nameproperty?

name财产在哪里??你在哪个动作类中声明了name财产?

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

它应该是 <s:property value="userName"/>

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

请记住,struts 会尝试通过将 get+YourProperty()

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

在您的情况下,尝试getName()在您的操作类中找出不可用的方法。

Edited:

编辑:

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

您的 urlhelloAction未正确映射,请尝试在浏览器中运行它,

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

which will become for your project as

这将成为您的项目

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

回答by subodh

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

是第一个和第二个,用于表单发布操作的用户 struts 属性

<s:form action="helloAction">

Hope will help you.

希望能帮到你。

回答by test30

beware: it is for Struts 1

当心:它适用于 Struts 1

I had the same problem, but I've solved mine by removing forwardproperty in action-mappings

我有同样的问题,但我已经通过删除forward属性解决了我的问题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 executeand will go straight to forward

但这将跳过调用execute并直接转到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>

回答by Russell Gutierrez

you should extend Actionin your action class:

你应该Action在你的动作类中扩展:

public class HelloWorldAction extends Action {

回答by someone

Try by extending com.opensymphony.xwork2.ActionSupportclass and override execute method like this

尝试通过扩展com.opensymphony.xwork2.ActionSupport类并覆盖这样的执行方法

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport{

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


}