gpt4 book ai didi

java - Struts 2 不调用 Action 类 execute() 方法

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:37 26 4
gpt4 key购买 nike

我是 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 在哪里属性(property)??您在哪个 Action 类中声明了name属性(property)?

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

记住struts 会尝试通过将get+YourProperty() 放在属性文件中找出getter 方法。

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

已编辑:

helloAction 的网址未正确映射尝试在浏览器中运行它,

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

这将成为您的项目

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

关于java - Struts 2 不调用 Action 类 execute() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12928017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com