Advertisement
Guest User

Untitled

a guest
Nov 12th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. package com.struts2hibernatepagination.hibernate;
  2.  
  3. import javax.persistence.Column;
  4. import javax.persistence.Entity;
  5. import javax.persistence.GeneratedValue;
  6. import javax.persistence.Id;
  7. import javax.persistence.Table;
  8.  
  9. @Entity
  10. @Table(name = "student")
  11. public class Student {
  12.  
  13. @Id
  14. @GeneratedValue
  15. private int id;
  16. @Column(name = "last_name")
  17. private String lastName;
  18. @Column(name = "first_name")
  19. private String firstName;
  20. private int marks;
  21.  
  22. public int getId() {
  23. return id;
  24. }
  25.  
  26. public void setId(int id) {
  27. this.id = id;
  28. }
  29.  
  30. public String getLastName() {
  31. return lastName;
  32. }
  33.  
  34. public void setLastName(String lastName) {
  35. this.lastName = lastName;
  36. }
  37.  
  38. public String getFirstName() {
  39. return firstName;
  40. }
  41.  
  42. public void setFirstName(String firstName) {
  43. this.firstName = firstName;
  44. }
  45.  
  46. public int getMarks() {
  47. return marks;
  48. }
  49.  
  50. public void setMarks(int marks) {
  51. this.marks = marks;
  52. }
  53.  
  54. }
  55.  
  56. package com.struts2hibernatepagination.action;
  57.  
  58. import java.util.ArrayList;
  59. import java.util.List;
  60.  
  61. import com.opensymphony.xwork2.ActionSupport;
  62. import com.opensymphony.xwork2.ModelDriven;
  63. import com.struts2hibernatepagination.dao.StudentDAO;
  64. import com.struts2hibernatepagination.hibernate.Student;
  65.  
  66. public class AddStudentAction extends ActionSupport implements
  67. ModelDriven<Student> {
  68.  
  69. public AddStudentAction() {
  70. // TODO Auto-generated constructor stub
  71. }
  72.  
  73. private Student student = new Student();
  74.  
  75. private List<Student> students = new ArrayList<Student>();
  76.  
  77. public Student getStudent() {
  78. return student;
  79. }
  80.  
  81. public void setStudent(Student student) {
  82. this.student = student;
  83. }
  84.  
  85. public List<Student> getStudents() {
  86. return students;
  87. }
  88.  
  89. public void setStudents(List<Student> students) {
  90. this.students = students;
  91. }
  92.  
  93. StudentDAO dao = new StudentDAO();
  94.  
  95. @Override
  96. public Student getModel() {
  97. // TODO Auto-generated method stub
  98. return student;
  99. }
  100. @Override
  101. public String execute() throws Exception {
  102. // TODO Auto-generated method stub
  103. dao.addStudent(student);
  104. String f = student.getFirstName();
  105. String l = student.getLastName();
  106. int m = student.getMarks();
  107. System.out.println(f + l + m + "Inside execute method");
  108. return "success";
  109. }
  110.  
  111. public String listStudents() {
  112. students = dao.getStudents();
  113. return "success";
  114. }
  115. @Override
  116. public void validate() {
  117. // TODO Auto-generated method stub
  118. if (student.getFirstName() == null) {
  119.  
  120. String first = student.getFirstName();
  121.  
  122. System.out.println(first);
  123. this.addActionError("Please Enter First Name !!!");
  124. System.out.println("Inside validate method!!");
  125. }
  126. // super.validate();
  127. }
  128. }
  129.  
  130. <%@ page contentType="text/html; charset=UTF-8"%>
  131. <%@ taglib prefix="s" uri="/struts-tags"%>
  132. <html>
  133. <head>
  134. <title>Hello World</title>
  135. <s:head />
  136. </head>
  137. <body>
  138. <s:form action="addStudent">
  139. <s:actionerror />
  140. <s:textfield name="firstName" label="First Name" />
  141. <s:textfield name="lastName" label="Last Name" />
  142. <s:textfield name="marks" label="Marks" />
  143. <s:submit />
  144. <hr />
  145. <table>
  146. <tr>
  147. <th>First Name</th>
  148. <th>Last Name</th>
  149. <th>Marks</th>
  150. </tr>
  151. <s:iterator value="students">
  152. <tr>
  153. <td><s:property value="firstName" /></td>
  154. <td><s:property value="lastName" /></td>
  155. <td><s:property value="marks" /></td>
  156. </tr>
  157. </s:iterator>
  158. </table>
  159.  
  160. </s:form>
  161. <s:form action="fetchStudentList">
  162. <s:submit>See All Student List</s:submit>
  163.  
  164. </s:form>
  165. </body>
  166. </html>
  167.  
  168. package com.struts2hibernatepagination.dao;
  169.  
  170. import java.util.ArrayList;
  171. import java.util.List;
  172.  
  173. import javax.transaction.Transaction;
  174.  
  175. import org.hibernate.Session;
  176.  
  177. import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
  178. import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;
  179. import com.struts2hibernatepagination.hibernate.Student;
  180.  
  181. public class StudentDAO {
  182.  
  183. // The session object and transaction object will be injected using the
  184. // @SessionTarget
  185. // and @TransactionTarget annotation respectively.
  186.  
  187. @SessionTarget
  188. Session session;
  189.  
  190. @TransactionTarget
  191. Transaction transaction;
  192.  
  193. @SuppressWarnings("unchecked")
  194. public List<Student> getStudents() {
  195. List<Student> students = new ArrayList<Student>();
  196. try {
  197. students = session.createQuery("from Student").list();
  198. } catch (Exception e) {
  199. e.printStackTrace();
  200. }
  201. return students;
  202. }
  203.  
  204. public void addStudent(Student student) {
  205. try {
  206. session.save(student);
  207. } catch (Exception e) {
  208. e.printStackTrace();
  209. }
  210. }
  211.  
  212. }
  213.  
  214. <?xml version="1.0" encoding="UTF-8"?>
  215. <!DOCTYPE struts PUBLIC
  216. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  217. "http://struts.apache.org/dtds/struts-2.0.dtd">
  218.  
  219. <struts>
  220. <constant name="struts.devMode" value="true" />
  221.  
  222. <package name="default" extends="hibernate-default">
  223.  
  224. <action name="addStudent" method="execute"
  225. class="com.struts2hibernatepagination.action.AddStudentAction">
  226.  
  227.  
  228.  
  229. <result name="success" type="redirect">
  230. listStudents
  231. </result>
  232.  
  233. <result name="input">/student.jsp</result>
  234.  
  235.  
  236.  
  237.  
  238. </action>
  239.  
  240. <action name="listStudents" method="listStudents"
  241. class="com.struts2hibernatepagination.action.AddStudentAction">
  242. <result name="success">/student.jsp</result>
  243. <result name="input">/student.jsp</result>
  244.  
  245.  
  246.  
  247.  
  248. </action>
  249.  
  250. <action name="fetchStudentList"
  251. class="com.struts2hibernatepagination.action.AddStudentAction"
  252. method="listStudents">
  253.  
  254. <result name="success">/displaytag.jsp</result>
  255. <result name="input">/student.jsp</result>
  256.  
  257.  
  258. </action>
  259.  
  260. </package>
  261.  
  262. <?xml version="1.0" encoding="UTF-8"?>
  263. <!DOCTYPE hibernate-configuration PUBLIC
  264. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  265. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  266. <hibernate-configuration>
  267. <session-factory>
  268. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  269. <property name="hibernate.connection.password">admin12345</property>
  270. <property name="hibernate.connection.url">jdbc:mysql://192.168.1.3:3306/nupur</property>
  271. <property name="hibernate.connection.username">root</property>
  272. <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  273. <property name="show_sql">true</property>
  274. <property name="hibernate.hbm2ddl.auto">update</property>
  275. <mapping class="com.struts2hibernatepagination.hibernate.Student" />
  276. </session-factory>
  277. </hibernate-configuration>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement