Wednesday, July 25, 2012

Struts2 Programs 001 model driven interface

I'll continue posting programs on this topic in the coming days . These programs are not my creation i have got these codes from the new so courtesy to all who has posted this code in the net

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_3_0.xsd"
id="WebApp_ID" version="3.0">

<display-name>Struts 2</display-name>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<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>
</web-app>

------------------------------------------------------------------------------------
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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />

<package name="kites" extends="struts-default">


<action name="addStudent"
class="com.kites.StudentAction" >
<result name="success">/addStudent.jsp</result>
</action>

<action name="studentAction"
class="com.kites.StudentAction" >
<result name="success">/studentInfo.jsp</result>
</action>

</package>

</struts>

-----------------------------------------------------------------------------------------------------------
StudentBean.java
--------------------------

package com.kites;

public class StudentBean{

String studentName;
int studentAge;

public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int getStudentAge() {
return studentAge;
}
public void setStudentAge(int studentAge) {
this.studentAge = studentAge;
}

}
----------------------------------------------------------------------------------
StudentAction.java
-----------------------------

package com.kites;

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

public class StudentAction extends ActionSupport
implements ModelDriven{

StudentBean student = new StudentBean();

public String execute() throws Exception {

return SUCCESS;

}

public Object getModel() {

return student;
}
}
-------------------------------------------------------------------------------------------------
addStudent.jsp
------------------------

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
<body>
<h1>ModelDriven Interface Example</h1>

<h2>Add Student</h2>
<s:form action="studentAction" >
<s:textfield name="studentName" label="Name" value=""/>
<s:textfield name="studentAge" label="Age" value=""/>
<s:submit />
</s:form>

</body>
</html>

---------------------------------------------------------------------------------------------------
studentInfo.jsp
-----------------------

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
<body>
<h1>ModelDriven Interface Example</h1>

<h2>Student Details</h2>
Name : <s:property value="studentName" /><br>
Age : <s:property value="studentAge" /><br>

</body>
</html>
--------------------------------------------------------------------------------------

No comments:

Post a Comment