Java Struts 2基于注释的应用程序不工作
我正在阅读《Struts2inAction》一书,在该书的第一章中,有一个将要开发的示例基本应用程序。我试着跟随它,但动作类调用不正确。但是我可以查看成功结果JSP页面。当我使用调试模式并尝试调试action类的execute方法时,它没有调用。我正在使用Java Struts 2基于注释的应用程序不工作,java,jsp,configuration,struts2,annotations,struts2-convention-plugin,Java,Jsp,Configuration,Struts2,Annotations,Struts2 Convention Plugin,我正在阅读《Struts2inAction》一书,在该书的第一章中,有一个将要开发的示例基本应用程序。我试着跟随它,但动作类调用不正确。但是我可以查看成功结果JSP页面。当我使用调试模式并尝试调试action类的execute方法时,它没有调用。我正在使用struts-2.3.15.1库来开发这个应用程序 我的行动课程如下: package manning.chapterTwo; import org.apache.struts2.config.Result; import com.open
struts-2.3.15.1
库来开发这个应用程序
我的行动课程如下:
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
):
如何解决它?您应该向action类或方法添加一个action注释
@ParentPackage("chapterTwo");
@Action(value="HelloWorld", results=@Result(name="SUCCESS", value="/chapterTwo/HelloWorld.jsp" ))
public class AnnotatedHelloWorldAction extends ActionSupport {
假设作者使用约定映射您的动作,或者在勘误表中添加了动作注释。还添加了操作所属的父包注释。不要忘记添加到库集中,因为示例中没有添加到库集中。有例外吗?你能分享一下服务器日志吗?如果你发了一篇文章,那会有点用。阅读起来并不容易。我还添加了struts2约定插件。Roman所说的修改无效,不会引发任何错误。但是action类的execute方法没有在调试模式下调用,结果也没有正确显示。导入org.apache.struts2.convention.annotation.Action;导入org.apache.struts2.convention.annotation.ParentPackage;导入org.apache.struts2.convention.annotation.Result;导入com.opensymphony.xwork2.ActionSupport@ParentPackage(“chapterTwo”)@Action(value=“/HelloWorld”,results=@Result(name=“SUCCESS”,location=“/chapterTwo/HelloWorld.jsp”))您是如何检查的?我使用eclipse作为ide,使用tomcat作为eclipse的服务器。我从eclipse以调试模式启动tomcat,并使用浏览器进行检查。
<%@ 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>
<?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 {