Friday, December 28, 2012

Apache Struts with Actions and Form Beans

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>swe642hw5khuc</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  <init-param>
  <param-name>config</param-name>
  <param-value>/WEB-INF/struts-config.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
    <form-beans>
    <form-bean name="surveyFormBean" type="package.SurveyFormBean"/>
    <form-bean name="surveyLookupFormBean" type="package.SurveyLookupFormBean"/>
    </form-beans>

    <action-mappings>
        <action path="/submitSurvey" type="package.SubmitSurveyAction" name="surveyFormBean" scope="session">        
        <forward name="simple-ack" path="/jsp/SimpleAcknowledgementJSP.jsp" />
        <forward name="winner-ack" path="/jsp/WinnerAcknowledgementJSP.jsp" />
        </action>
        <action path="/displaySurvey" type="package.DisplaySurveyAction" scope="session">
        <forward name="survey-found" path="/jsp/StudentJSP.jsp" />
        <forward name="survey-not-found" path="/jsp/NoSuchStudentJSP.jsp" />
        </action>
    </action-mappings>
</struts-config>

SubmitSurveyAction.java
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class SubmitSurveyAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception{
SurveyFormBean formBean = (SurveyFormBean)form;

StudentBean studentBean = new StudentBean(formBean.getStudentId(),formBean.getFirstName(), formBean.getLastName(),
formBean.getStreet(), formBean.getZip(), formBean.getCity(), formBean.getState(),
formBean.getTelephone(), formBean.getEmail(), formBean.getUrl(), formBean.getDateOfSurvey(),
formBean.getGraduationMonth(), formBean.getGraduationYear(), formBean.getLikes(),
formBean.getInterests(), formBean.getRecommendation(), formBean.getComments());

StudentDAO.SaveStudentSurveyFormData(studentBean);

DataBean dataBean = DataProcessor.ComputeMeanAndStandardDeviation(formBean.getData());

List<String> surveyIdsBean = StudentDAO.GetSurveyIds();

HttpSession session = request.getSession();

session.setAttribute("student", studentBean);
session.setAttribute("data", dataBean);
session.setAttribute("surveys", surveyIdsBean);

if(dataBean.getMean() > 90){
return mapping.findForward("winner-ack");
}
return mapping.findForward("simple-ack");
}
}

DisplaySurveyAction.java
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class DisplaySurveyAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception{
String studentId = request.getParameter("studentId");
StudentBean studentBean = null;

if(studentId != null){
studentBean = StudentDAO.GetStudentSurveyData(studentId);
}

HttpSession session = request.getSession();

if(studentBean == null){
session.setAttribute("badId", studentId);
return mapping.findForward("survey-not-found");
}
else{
session.setAttribute("student", studentBean);
return mapping.findForward("survey-found");
}
}
}

SurveyFormBean.java
import org.apache.struts.action.ActionForm;
@SuppressWarnings("serial")
public class SurveyFormBean extends ActionForm {
private String studentId;
private String firstName;
private String lastName;
private String street;
private String zip;
private String city;
private String state;
private String telephone;
private String email;
private String url;
private String dateOfSurvey;
private String graduationMonth;
private String graduationYear;
private String[] likes;
private String interests;
private String recommendation;
private String comments;
private String data;

public String getStudentId(){
return this.studentId;
}

public void setStudentId(String studentId){
this.studentId = studentId;
}

public String getFirstName(){
return this.firstName;
}

public void setFirstName(String firstName){
this.firstName = firstName;
}

public String getLastName(){
return this.lastName;
}

public void setLastName(String lastName){
this.lastName = lastName;
}

public String getStreet(){
return this.street;
}

public void setStreet(String street){
this.street = street;
}

public String getZip(){
return this.zip;
}

public void setZip(String zip){
this.zip = zip;
}

public String getCity(){
return this.city;
}

public void setCity(String city){
this.city = city;
}

public String getState(){
return this.state;
}

public void setState(String state){
this.state = state;
}

public String getTelephone(){
return this.telephone;
}

public void setTelephone(String telephone){
this.telephone = telephone;
}

public String getEmail(){
return this.email;
}

public void setEmail(String email){
this.email = email;
}

public String getUrl(){
return this.url;
}

public void setUrl(String url){
this.url = url;
}

public String getDateOfSurvey(){
return this.dateOfSurvey;
}

public void setDateOfSurvey(String dateOfSurvey){
this.dateOfSurvey = dateOfSurvey;
}

public String getGraduationMonth(){
return this.graduationMonth;
}

public void setGraduationMonth(String graduationMonth){
this.graduationMonth = graduationMonth;
}

public String getGraduationYear(){
return this.graduationYear;
}

public void setGraduationYear(String graduationYear){
this.graduationYear = graduationYear;
}

public String[] getLikes(){
return this.likes;
}

public void setLikes(String[] likes){
this.likes = likes;
}

public String getInterests(){
return this.interests;
}

public void setInterests(String interests){
this.interests = interests;
}

public String getRecommendation(){
return this.recommendation;
}

public void setRecommendation(String recommendation){
this.recommendation = recommendation;
}

public String getComments(){
return this.comments;
}

public void setComments(String comments){
this.comments = comments;
}

public String getData(){
return this.data;
}

public void setData(String data){
this.data = data;
}
}

No comments: