HibernateJavaStruts2

Struts 2, Hibernate 4 and MySQL Integration with Maven (Part 2)

Let’s modify the application code now to create a New Employee page and add corresponding Action class and mappings.

Step 1:


Create a new jsp file NewEmployee.jsp under WEB-INF/example folder.

<%@ page contentType=“text/html; charset=UTF-8”%>

<%@ taglib prefix=“s”uri=“/struts-tags”%>

<html>

<head>

<title><s:text name=“HelloWorld.message”/></title>

</head>

<body>

<s:actionerror/>

<s:form action=“register”>

<s:textfield name=“name”label=“Name”></s:textfield>

<s:textfield name=“age”label=“Age”></s:textfield>

<s:submit/>

</s:form>

</body>

</html>

Also, create another jsp file RegisterSuccess.jsp in same path. This page will be displayed on success of add employee.

<%@ page contentType=“text/html; charset=UTF-8”%>

<%@ taglib prefix=“s”uri=“/struts-tags”%>

<html>

<head>

<title><s:text name=“HelloWorld.message”/></title>

</head>

<body>

<h2>Welcome, <s:property value=“name”></s:property></h2>

</body>

</html>

Step 2:


Create new package com.techkatak.bean and add bean class EmployeeBean.java in this package

Add two properties name and age in this bean. Here is the code :

package com.techkatak.bean;

publicclass EmployeeBean {

String name;

intage;

public String getName() {

returnname;

}

publicvoid setName(String name) {

this.name = name;

}

publicint getAge() {

returnage;

}

publicvoid setAge(int age) {

this.age = age;

}

}

 

Step 3:

Create new package com.techkatak.action and add RegisterAction.java in this package.

package com.techkatak.action;

import com.techkatak.bean.EmployeeBean;

import com.techkatak.dao.EmployeeDAO;

publicclass RegisterAction

{

private String name;

private String age;

public String execute(){

EmployeeBean emp = new EmployeeBean();

emp.setName(getName());

emp.setAge(Integer.parseInt(getAge()));

EmployeeDAO dao = new EmployeeDAO();

dao.addEmployee(emp);

return“success”;

}

public String getName() {

returnname;

}

publicvoid setName(String name) {

this.name = name;

}

public String getAge() {

returnage;

}

publicvoid setAge(String age) {

this.age = age;

}

}

 

Step 4:

Update Struts configuration file

From the  generated struts configuration file, we will remove the include for example.xml and add mappings for NewEmployee.jsp and RegisterAction.java

Here is the final struts.xml file.

<?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=“true”/>

<package name=“default” namespace=“/” extends=“struts-default”>

<default-action-ref name=“index”/>

<action name=“index”>

<result type=“redirectAction”>

<param name=“actionName”>NewEmployee</param>

<param name=“namespace”>/example</param>

</result>

</action>

<action name=“NewEmployee” class=“techkatak.example.HelloWorld”>

<result >/WEB-INF/example/NewEmployee.jsp</result>

</action>

<action name=“register” class=“com.techkatak.action.RegisterAction”>

<result >/WEB-INF/example/NewEmployee.jsp</result>

<result name=“success”>/WEB-INF/example/RegisterSuccess.jsp</result>

</action>

</package>

</struts>

Now the changes to pages, action class and struts configuration is complete.

In the last part of the tutorial, we integrate hibernate & MySQL

Here is the link to part 3 of this tutorial :

Struts 2, Hibernate 4 and MySQL Integration with Maven (Part 3)

© 2015, www.techkatak.com. All rights reserved.

One thought on “Struts 2, Hibernate 4 and MySQL Integration with Maven (Part 2)

Comments are closed.