簡體   English   中英

Struts 2沒有調用action類的execute()方法

[英]Struts 2 not calling action class execute() method

我是Struts2的新手,並在struts中創建了一個簡單的HelloWorld應用程序,但問題是我點擊提交按鈕時沒有調用我的動作類,控制台上也沒有任何異常。 這是我的代碼,

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"/>

name屬性在哪里? 你在哪個動作類中聲明了name屬性?

它應該是<s:property value="userName"/>

記得struts會嘗試通過放置get+YourProperty()來找出屬性文件的getter方法

在你的情況下,它會嘗試在你的動作類中找到不可用的getName()方法。

編輯:

你的helloAction網址未正確映射嘗試在瀏覽器中運行

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

這將成為你的項目

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

我認為您需要在代碼中進行兩處更改

public class HelloWorldAction extends Action 

是第一個和第二個,用戶struts屬性表單發布動作

<s:form action="helloAction">

希望能幫到你。

你應該在你的動作類中擴展Action

public class HelloWorldAction extends Action {

嘗試通過擴展com.opensymphony.xwork2.ActionSupport類並覆蓋這樣的execute方法

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport{

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


}

要注意: 它適用於Struts 1

我有同樣的問題,但我通過刪除action-mappings forward屬性來解決我的問題

這個是正確的:

<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>

但這將跳過調用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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM