This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Hibernate LoginForm with Struts 2 framwork Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate LoginForm with Struts 2 framwork" Watch "Hibernate LoginForm with Struts 2 framwork" New topic
Author

Hibernate LoginForm with Struts 2 framwork

Devendra Bhati
Greenhorn

Joined: Dec 27, 2012
Posts: 19
Devendra Bhati wrote:I'm getting an Error :


Struts Problem Report
Struts has detected an unhandled exception:

Messages: Unable to intantiate Action!



--------------------------------------------------------------------------------

Stacktraces
Unable to intantiate Action!
com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:289)
com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:387)
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:186)
org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:458)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
java.lang.Thread.run(Unknown Source)

java.lang.InstantiationException
sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(Unknown Source)
java.lang.reflect.Constructor.newInstance(Unknown Source)
java.lang.Class.newInstance0(Unknown Source)
java.lang.Class.newInstance(Unknown Source)
com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:119)
com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:150)
com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:139)
com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:109)
com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:287)
com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:387)
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:186)
org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:458)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
java.lang.Thread.run(Unknown Source)




Here my complete program code is :


+----------+----------+-------+
| userName | password | role |
+----------+----------+-------+
| dev | dev | admin |
| sapna | sapna | guest |
+----------+----------+-------



web.xml

<?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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<display-name>HibernateLogin</display-name>

<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


index.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>

<title>Struts 2 - Hello World</title>
</head>

<body>
<h2 class="heading">
<u>Account Sign in</u>
</h2>
<s:if test="hasActionErrors()">
<div>
<s:actionerror />
</div>
</s:if>
<s:form name="frmLogin" action="login">
<s:textfield id="userName" name="userName" label="User Name"></s:textfield>
<s:password name="password" label="Password"></s:password>
<s:submit align="center"></s:submit>
</s:form>
</body>
</html>

Welcome.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h2>Hello, <s:property value="login.getRole()" />...!</h2>
</body>
</html>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="com.HibernateLogin"
namespace="/"
extends="struts-default">
<action name="login" class="com.HibernateLogin.LoginAction" method="checkLogin">
<result name="success">/Welcome.jsp</result>

<result name="error">/index.jsp</result>

</action>


</package>

</struts>

Login.java
package com.HibernateLogin;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table (name = "TBL_USER")
public class Login implements Serializable{

private String userName;
private String password;
private String role;
/**
* @return the userName
*/
@Id
@Column (name = "V_USER_NAME")
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the password
*/
@Column (name = "V_PASSWORD")
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the role
*/
@Column (name = "V_ROLE")
public String getRole() {
return role;
}
/**
* @param role the role to set
*/
public void setRole(String role) {
this.role = role;
}
}

LoginAction.java

package com.HibernateLogin;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("rawtypes")
public abstract class LoginAction extends ActionSupport implements ModelDriven {

private Login login = null;
private static final long serialVersionUID = 1L;
private LoginDAO loginDAO = new LoginDAO();

public String execute(){
return SUCCESS;
}

public String checkLogin() {
if(login.getUserName().equals("") || login.getPassword().equals("")){
addActionError("Please Enter All Values");
return ERROR;
}else {

login = loginDAO.checkLogin(login);
if(null != login.getRole()){
return SUCCESS;

}else {
addActionError("Invalid User ID / PAssword.");
return ERROR;
}
}
}

public Object getmodel(){
login = new Login();
return login;
}

public Login getLogin() {
return login;
}

public void setLogin(Login login) {
this.login = login;
}

}

LoginDAO.java

package com.HibernateLogin;

import java.util.Iterator;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class LoginDAO {

public Login checkLogin(Login login){
Session session = new Configuration().configure().buildSessionFactory().openSession();

Transaction transaction = null;
String userName = null;
String password = null;

userName = login.getUserName();
password = login.getPassword();

String SQL_QUERY = "SELECT login FROM Login login WHERE login.userName = '"+
userName+"' AND login.password = '"+password+"'";
try{
System.out.println(SQL_QUERY);
transaction = session.beginTransaction();
Query query = session.createQuery(SQL_QUERY);


Iterator it = query.iterate();

if(it.hasNext()){
login = (Login)it.next();
}else{
login.setRole(null);
}

}catch(Exception e){
System.out.println(e.getMessage());
}

return login;
}
}


hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="com.HibernateLogin.Login" />
</session-factory>
</hibernate-configuration>
 
 
subject: Hibernate LoginForm with Struts 2 framwork
 
Similar Threads
Problems deploying struts 2 app on weblogic 10gR3
The requested resource () is not available
Login Form
Http Server 404
[org.apache.struts2.dispatcher.Dispatcher] Could not find action or result Error