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