1

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. 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. 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! 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. 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. environment (if that would be helpful).

I have everything installed and working, although JDBC is confusing to me. 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 :

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:

enter image description here

This is my 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 :

<?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 :

  <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 :

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Jason Rodriguez
  • 183
  • 1
  • 1
  • 13

1 Answers1

1

Change the filter class in web.xml to org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter. The FilderDispatcher is deprecated and as you using 2.3 DTD it should correspond to libraries.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • It's something with eclipse project settings of the Tomcat configuration for deployment, you should configure at which context you will deploy your app. – Roman C Nov 27 '13 at 19:38
  • Thanks for the heads up! Didn't change any errors but thanks! I just added my controller and view to give you more of something to work with – Jason Rodriguez Nov 27 '13 at 19:40
  • You need to make a deployment or deploy you application to the server Tomcat if you have configured Tomcat in Eclipse. – Roman C Nov 27 '13 at 19:43
  • By deploy, do you mean right click and run on server? I did that and that is where I am getting my errors from – Jason Rodriguez Nov 27 '13 at 19:44
  • Check the servers tab which should include the server to deploy right click on it if exist and add deployment. – Roman C Nov 27 '13 at 19:46
  • May be my suggestions aren't precise because I worked with eclipse long time ago even with MyEclipse where it has embedded Tomcat and a button to deploy the project. This was absent with other free versions. In the newer versions something called web deployment assembly is used. – Roman C Nov 27 '13 at 19:53
  • I see, well how would you zero in on the problem? Perhaps if I had a direction, I can try and work it out in my code – Jason Rodriguez Nov 27 '13 at 19:54
  • Run on server is good but Eclipse should know which server to run, if you open the server you should see your project webapp in it and as I said it should have a web app context. I don't remember may be some web facet is used for it. – Roman C Nov 27 '13 at 20:00
  • My bad, I just realized I asked the question incorrectly, the app does compile and run. What I meant to say is it gives me an error 404 page! Sorry, I'll correct it in my title now – Jason Rodriguez Nov 27 '13 at 20:03
  • Enter URL on which an action is mapped, i.e. `index` and use context path at which you have deployed and I talked about. – Roman C Nov 27 '13 at 20:09
  • When 404 error the first question is what URL you enter in the browser navigator? – Roman C Nov 27 '13 at 20:28
  • Cleaning the app and the server did the trick! Thanks so much! I am now getting new warning tags but no severe errors :) I will work through the app to see if it is working properly but I do have two pressing questions. First, how can I check my database to ensure that my input is being recorded into it properly?Second, how can I redirect to my list.jsp after someone clicks submit? This conincides with my need for a `back to home` link I also need to add. Normally I'd to an `` but not sure if that is the right approach here. What do you think? – Jason Rodriguez Nov 27 '13 at 20:32
  • The corresponding perspective like Hibernate could manage datasources that you like to explore (the feature is org.eclipse.datatools). Actions could return a result type "redirect" or "redirectAction". – Roman C Nov 27 '13 at 20:37
  • 1
    Also a `chain` result type is discouraged, don't use it without a proper reason. – Roman C Nov 27 '13 at 20:46
  • thank you for being super helpful. I will try my luck implementing some of these changes and see what happens. I'll come back to this thread and update the progress in a bit! – Jason Rodriguez Nov 27 '13 at 21:02
  • Do you have a progress on it? – Roman C Dec 03 '13 at 16:10
  • Hey Roman,as it turns out, there were several other issues that arose and it was becoming more trouble than it was worth since I am a novice with limited understanding. So, I decided to rewrite the app through a simple struts2 / jdbc model. I have encountered a small snag with it though and I was hoping you could chime in, here is the link : http://stackoverflow.com/questions/20361792/browser-wont-show-home-page-when-running-tomcat-in-eclipse-for-a-simple-struts2 – Jason Rodriguez Dec 03 '13 at 21:16
  • I will say though, I do want ot figure out hte hibernate app though. So, if I can get this struts2/jdbc app working, I will definitely go back to this and maybe we can get this hibernate version up and running! – Jason Rodriguez Dec 03 '13 at 21:18