home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="S" %>
<S:form action="register">
<S:textfield name="name" label="Name"></S:textfield>
<S:textfield name="email" label="email"></S:textfield>
<S:textfield name="password" label="password"></S:textfield>
<S:textfield name="amount" label="amount"></S:textfield>
<S:submit value="register"></S:submit>
</S:form>
welcome.jsp
<%@ taglib uri="/struts-tags" prefix="S" %>
Welcome: <S:property value="name"/>
Login.java
package com.java;
import java.util.ArrayList;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Login
{
public static ArrayList<Testing> saveUser(Testing name)
{
String fullname = name.getName();
String pwd=name.getPassword();
String mail=name.getEmail();
int amt=name.getAmount();
ArrayList<Testing> student = new ArrayList<Testing>();
name.setName(fullname);
name.setPassword(pwd);
name.setEmail(mail);
name.setAmount(amt);
student.add(name);
return student;
}
}
Testing.java
package com.java;
import java.util.ArrayList;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.opensymphony.xwork2.ActionSupport;
public class Testing extends ActionSupport {
private static final long serialVersionUID = 1L;
private String name,password,email;
private int amount;
public String execute()
{
ArrayList<Testing> student = Login.saveUser(this);
Testing testing = student.get(0);
Session session=new Configuration().
configure("hibernate.cfg.xml").buildSessionFactory().openSession();
Transaction t=session.beginTransaction();
session.save(testing);
t.commit();
session.close();
return "success";
}
//use getter and setter here
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation
//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="register" class="com.java.Testing">
<result name="success">welcome.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<welcome-file-list>
<welcome-file>home.jsp</welcome-file>
</welcome-file-list>
<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>
Testing.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jul 6, 2016 2:51:32 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.java.Testing" table="testing">
<id name="name" type="string">
<column name="Name" length="50" />
<generator class="assigned" />
</id>
<property name="password" type="string">
<column name="Password" length="50" />
</property>
<property name="email" type="string">
<column name="email" length="50" />
</property>
<property name="amount" type="int">
<column name="Amount" length="11" />
</property>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<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/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="Testing.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Very nice ....Explanation....Thank You.....
ReplyDelete