gpt4 book ai didi

java - Struts 2 基于注释的应用程序不工作

转载 作者:行者123 更新时间:2023-12-01 04:27:23 24 4
gpt4 key购买 nike

我正在阅读《Struts 2 in Action》一书,在该书的第一章中,有一个需要开发的示例基本应用程序。我尝试遵循它,但操作类未正确调用。但我可以在JSP页面中查看成功结果。当我使用 Debug模式并尝试调试操作类的执行方法时,它没有调用。我正在使用 struts-2.3.15.1 库来开发此应用程序。

我的 Action 类如下:

package manning.chapterTwo;

import org.apache.struts2.config.Result;

import com.opensymphony.xwork2.ActionSupport;



@Result(name="SUCCESS", value="/chapterTwo/HelloWorld.jsp" )
public class AnnotatedHelloWorldAction extends ActionSupport{


private static final long serialVersionUID = 1L;

private static final String GREETING = "Hello ";


public String execute() {

setCustomGreeting( GREETING + getName() );

return "SUCCESS";
}

private String name;


public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

private String customGreeting;

public String getCustomGreeting()
{
return customGreeting;
}

public void setCustomGreeting( String customGreeting ){
this.customGreeting = customGreeting;
}
}

用于获取输入的 JSP 页面如下所示 (NameCollector.jsp):

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>

<head>
<title>Name Collector</title>
</head>

<body>
<hr>
<h4>Enter your name so that we can customize a greeting just for you!</h4>
<s:form action="annotatedHelloWorld">
<s:textfield name="name" label="Your name"/>
<s:submit/>
</s:form>
<hr>
</body>

</html>

输出JSP:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>HelloWorld</title>
</head>

<body>

<hr>
<h3>Custom Greeting Page</h3>
<h4><s:property value="customGreeting"/></h4>
<hr>
</body>
</html>

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" 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">
<display-name>Struts2HelloWorldXML</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>manning</param-value>
</init-param>
</filter>

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

</web-app>

如何解决?

最佳答案

您应该向操作类或方法添加操作注释

@ParentPackage("chapterTwo");
@Action(value="HelloWorld", results=@Result(name="SUCCESS", value="/chapterTwo/HelloWorld.jsp" ))
public class AnnotatedHelloWorldAction extends ActionSupport {

假设作者使用约定来映射您的操作或在勘误表中添加操作注释。还添加了action所属的父包注解。并且不要忘记添加 到库集,因为示例中遗漏了它。

关于java - Struts 2 基于注释的应用程序不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18398062/

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