Passing object from jsp page to action using model driven
Project Explorer addstudent.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Student Registration</title> </head> <body> <h1>Enter Student Details</h1> <s:form action="add-student" method="post"> <s:textfield name="name" label="Name" placeholder="Name of the student"/> <s:textfield name="rollNo" label="Roll No" placeholder="Roll no of the student"/> <s:textfield name="branch" label="Branch" placeholder="Branch of the student"/> <s:textfield name="semester" label="Semester " placeholder="Semester of the student"/> <s:submit value="Submit Detail"/> </s:form> </body> </html> |
struts.xml
1 2 3 4 5 6 7 8 9 10 11 | <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" extends="struts-default"> <action name="add-student" class="com.ebhor.StudentAction"> <result name="success">/pages/showstudent.jsp</result> </action> </package> </struts> |
StudentAction.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package com.ebhor; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class StudentAction extends ActionSupport implements ModelDriven<Student> { private Student student=new Student(); public String execute() { System.out.println("Name " + getStudent().getName() + " RollNo " + getStudent().getRollNo() + " Branch " + getStudent().getBranch() + " Smester " + getStudent().getSemester()); return SUCCESS; } @Override public Student getModel() { return student; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } } |
Student.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | package com.ebhor; public class Student implements java.io.Serializable { private String name; private String rollNo; private String branch; private String semester; public Student() { } public Student(String name, String rollNo, String branch, String semester) { this.name = name; this.rollNo = rollNo; this.branch = branch; this.semester = semester; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRollNo() { return rollNo; } public void setRollNo(String rollNo) { this.rollNo = rollNo; } public String getBranch() { return branch; } public void setBranch(String branch) { this.branch = branch; } public String getSemester() { return semester; } public void setSemester(String semester) { this.semester = semester; } } |
showstudent.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Student Details</title> </head> <body> <h1>Student Detail</h1> Name: <s:property value="name"/><br/> Roll No: <s:property value="rollNo"/><br/> Branch: <s:property value="branch"/><br/> Semester: <s:property value="semester"/><br/> </body> </html> |
OUTPUT