2

I have tried everything to run my first Struts2 demo application but i am failed to run it. I have followed the tutorial on

http://www.quickprogrammingtips.com/struts2/struts-2-netbeans-tutorial.html

Here is my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 version="3.1">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

      <display-name>Struts2 Demo App</display-name>
    <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>

Here is my struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="Struts2Demo" extends="struts-default">
        <action name="HelloWorld" class="controller.HelloWorld">
            <result>/message.jsp</result>
        </action>
        <!-- Add your actions here -->
    </package>
</struts>

here is controller file

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import com.opensymphony.xwork2.ActionSupport;
import model.Message;

public class HelloWorld extends ActionSupport {
    private Message message;

    @Override
    public String execute() {
        setMessage(new Message()); // get data from model
        return SUCCESS;
    }



      public Message getMessage() {
            return message;
        }

        public void setMessage(Message message) {
            this.message = message;
        }

    }

Here is my jsp file

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="Struts2Demo" extends="struts-default">
        <action name="HelloWorld" class="controller.HelloWorld">
            <result>/message.jsp</result>
        </action>
        <!-- Add your actions here -->
    </package>
</struts>

and here is the model file

   /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

public class Message {
    private String message = "Hello World!";

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

My file structure is

enter image description here

Netbeans version: 8.1 Glassfish server: 4 All struts2 configuration are setup manually without any plugin

4

1 回答 1

3

您的启动日志准确地指出了问题所在,尽管您会遇到更多相同的问题,因为您没有部署所有必需的依赖项。

对于您的应用,您至少需要:

WEB-INF/lib
├── commons-fileupload-1.3.1.jar
├── commons-io-2.2.jar
├── commons-lang3-3.2.jar
├── commons-logging-1.1.3.jar
├── freemarker-2.3.22.jar
├── javassist-3.11.0.GA.jar
├── ognl-3.0.6.jar
├── struts2-core-2.3.24.1.jar
└── xwork-core-2.3.24.1.jar

这就是为什么您应该使用 Maven、Gradle 或等效的依赖管理器的原因。Java 应用程序使用的每个库都有自己的依赖项,而您缺少很多依赖项。

我使用http://mvnrepository.com/手动获取您的应用程序所需的库,使用启动错误消息作为指导。这是一项你应该掌握的技能,即使它是由 Maven/etc 为你管理的。在不知道它处理什么的情况下,您会倾向于认为您不需要它。

于 2015-10-02T20:27:41.177 回答