简体   繁体   English

在MySQL中使用Struts 2和Eclipse进行Hibernate编译时,App在Tomcat上显示404错误

[英]App gives 404 error on Tomcat when compiled in MySQL with Struts 2 and Hibernate on Eclipse

I am attempting to run a simple Struts app that presents fields for the user to enter which OS type and version they use with an optional field for notes. 我正在尝试运行一个简单的Struts应用程序,该应用程序显示字段供用户输入他们使用的操作系统类型和版本,以及用于注释的可选字段。 It will then present the results on a new page as an indexed list. 然后它将结果作为索引列表显示在新页面上。 It would be analogous to any basic contact organizer app but for listing OS info. 这将类似于任何基本的联系人组织者应用程序,只是列出了操作系统信息。

However, the IDE shows no errors at all. 但是,IDE完全不显示任何错误。 I think I am not connecting to the db correctly. 我认为我没有正确连接到数据库。 This is the step I was most unsure of when setting up as well. 这也是我在设置时最不确定的步骤。 Since there are specific frameworks, tools and such I am using, I was unable to find a tutorial that pertained specifically to setting up a db in my environment(not sure if there is a diff or if there is a universal approach). 由于存在特定的框架,工具和我正在使用的工具,因此无法找到专门与在我的环境中设置数据库有关的教程(不确定是否存在差异或是否存在通用方法)。

Since this is the first app I have ever built in Java, my troubleshooting ability is pretty limited but I am giving it all I got! 由于这是我用Java构建的第一个应用程序,因此我的故障排除能力非常有限,但我会尽我所能! It is a big jump (for me) coming from Rails/JS and so a bit of guidance from you Jedis to a Padawan like me will go a long way. 来自Rails / JS对我来说是一个巨大的飞跃,因此从您Jedis到像我这样的Padawan的一些指导将走很长一段路。 Anyway, since it can be tricky to jump into a Java code base(in my opinion), I'll be as precise as possible but feel free to drop me a line for elaboration, need to view a specific file, or if you just want a war file of my project to check it out in your own dev. 无论如何,由于跳入J​​ava代码库可能很棘手(我认为),因此我会尽可能地精确,但是请随时给我下一行进行阐述,需要查看特定文件,或者如果您只是想要我的项目的战争文件在您自己的开发中检出。 environment (if that would be helpful). 环境(如果有帮助的话)。

I have everything installed and working, although JDBC is confusing to me. 尽管JDBC使我感到困惑,但我已安装并正常工作。 Is it something you install manually or you call it in your code as a dependency? 它是您手动安装的东西,还是您在代码中将其称为依赖项? When I try to compile and run using Tomcat 7, There are a few errors that basically say the same thing as the snippet below points out : 当我尝试使用Tomcat 7进行编译和运行时,存在一些错误,这些错误基本上与下面的代码片段指出的一样:

SEVERE: Dispatcher initialization failed
Unable to load configuration. - action - file:/Users/jasonrodriguez/Java/apache-tomcat-7.0.47/wtpwebapps/firstapp/WEB-INF/classes/struts.xml:14:74

at different points in the code base. 在代码库的不同位置。 So perhaps they are all connected to the same problem. 因此,也许它们都与同一个问题有关。

File Structure: 档案结构:

在此处输入图片说明

This is my hibernate.cfg.xml : 这是我的hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 4.3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-4.3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="connection.url">
            jdbc:mysql://localhost:3306/UserManager
        </property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.pool_size">1</property>
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="current_session_context_class">thread</property>
        <property name="cache.provider_class">
            org.hibernate.cache.NoCacheProvider
        </property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>

        <mapping class="net.jasonrodriguez.user.model.User" />

    </session-factory>

This is my struts.xml : 这是我的struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <constant name="struts.enable.DynamicMethodInvocation"
        value="false" />
    <constant name="struts.devMode" value="false" />

    <package name="default" extends="struts-default" namespace="/">

        <action name="add"
            class="net.jasonrodriguez.user.view.UserAction" method="add">
            <result name="success" type="chain">index</result>
            <result name="input" type="chain">index</result>
        </action>

        <action name="delete"
            class="net.jasonrodriguez.user.view.UserAction" method="delete">
            <result name="success" type="chain">index</result>
        </action>

        <action name="index"
            class="net.jasonrodriguez.user.view.UserAction">
            <result name="success">index.jsp</result>
        </action>
    </package>
</struts>

Here is my web.xml filter for Struts : 这是我的Struts的web.xml过滤器:

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
        org.apache.struts2.dispatcher.FilterDispatcher
    </filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

Here is my controller : 这是我的控制器:

package net.jasonrodriguez.user.controller;


import java.util.List;

import net.jasonrodriguez.user.model.User;
import net.jasonrodriguez.user.util.HibernateUtil;

import org.hibernate.HibernateException;
import org.hibernate.Session;

public class UserManager extends HibernateUtil {

    public User add(User user) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();
        return user;
    }
    public User delete(Long id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        User user = (User) session.load(User.class, id);
        if(null != user) {
            session.delete(user);
        }
        session.getTransaction().commit();
        return user;
    }

    public List<User> list() {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<User> users = null;
        try {

            users = (List<User>)session.createQuery("from User").list();

        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        session.getTransaction().commit();
        return users;
    }
}

This is my HibernateUtil.java : 这是我的HibernateUtil.java

package net.jasonrodriguez.user.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new AnnotationConfiguration().configure()
                    .buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

Here is my view action : 这是我的查看动作:

package net.jasonrodriguez.user.view;

import java.util.List;

import net.jasonrodriguez.user.controller.UserManager;
import net.jasonrodriguez.user.model.User;

import com.opensymphony.xwork2.ActionSupport;


public class UserAction extends ActionSupport {

    private static final long serialVersionUID = 9149826260758390091L;
    private User user;
    private List<User> userList;
    private Long id;

    private UserManager userManager;

    public UserAction() {
        userManager = new UserManager();
    }

    public String execute() {
        this.userList = userManager.list();
        System.out.println("execute called");
        return SUCCESS;
    }

    public String add() {
        System.out.println(getUser());
        try {
            userManager.add(getUser());
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.userList = userManager.list();
        return SUCCESS;
    }

    public String delete() {
        userManager.delete(getId());
        return SUCCESS;
    }

    public User getUser() {
        return user;
    }

    public List<User> getUserList() {
        return userList;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void setUserList(List<User> usersList) {
        this.userList = usersList;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

When Tomcat is deployed, it gives me a web page with a 404 error saying it cannot locate the resource. 部署Tomcat后,它为我提供了一个带有404错误的网页,提示它找不到资源。

Change the filter class in web.xml to org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter . web.xml的过滤器类更改为org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter The FilderDispatcher is deprecated and as you using 2.3 DTD it should correspond to libraries. 不建议使用FilderDispatcher ,并且在使用2.3 DTD时,它应该对应于库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM