give an example of html:multibox which takes contents from an arraylist having getter and setter methods in form class. the select values should be be stored in another arraylist or string array and should be displayed on next jsp page on submit.
plz. reply quickly.
1)index.jsp:
<html> <head> <title>Loading..........</title> <meta http-equiv="refresh" content="0;URL='displayques'" > </head> <body> Loading..... </body> </html>
2)Questions.jsp:
<%@taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Question</title> <s:head/> </head> <body> <h1>Questions </h1><hr/> <s:form action="questionaction"> <s:submit /> </s:form> </body> </html>
3)QuestionAction.java:
package net.roseindia.action; import java.util.Iterator; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import net.roseindia.dao.QuestionDAO; import net.roseindia.model.QuestionModel; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class QuestionAction extends ActionSupport implements ModelDriven { QuestionModel obQuestionModel; QuestionDAO obQuestionDAO = new QuestionDAO(); List obQuestions; ActionContext context=ActionContext.getContext(); HttpServletRequest request=(HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST); @Override public String execute() throws Exception { obQuestions = obQuestionDAO.allQuestion(); request.setAttribute("questionList", obQuestions); return SUCCESS; } @Override public Object getModel() { // TODO Auto-generated method stub obQuestionModel = new QuestionModel(); return obQuestionModel; } public List getObQuestions() { return obQuestions; } public void setObQuestions(List obQuestions) { this.obQuestions = obQuestions; } }
4)QuestionDao.java:
package net.roseindia.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import net.roseindia.connectionfactory.ConnectionFactory; import net.roseindia.model.QuestionModel; public class QuestionDAO { Connection con = null; PreparedStatement pst = null; ResultSet rs = null; public Connection getConnection() { Connection conn; conn = ConnectionFactory.getInstance().getConnection(); return conn; } public List<QuestionModel> allQuestion() { List<QuestionModel> obQuestionModels= new ArrayList<QuestionModel>(); try { con = getConnection(); String query = "select quesno,question from new_ques limit 4"; pst = con.prepareStatement(query); rs=pst.executeQuery(); while (rs.next()) { QuestionModel obModel=new QuestionModel(); obModel.setQuestion(rs.getString(2)); obQuestionModels.add(obModel); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return obQuestionModels; } }
5)ConnectionFactory.java:
package net.roseindia.connectionfactory; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class ConnectionFactory { private String driverName = "com.mysql.jdbc.Driver"; private String conUrl = "jdbc:mysql://192.168.10.13:3306/onlinexamination"; private String conUser="root"; private String conPass="root"; private static ConnectionFactory connectionFactory=null; public ConnectionFactory() { try { Class.forName(driverName); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public Connection getConnection() { Connection conn=null; try { conn=DriverManager.getConnection(conUrl,conUser,conPass); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static ConnectionFactory getInstance() { if (connectionFactory==null) { connectionFactory=new ConnectionFactory(); } return connectionFactory; } }
6)Questionlist.jsp:
<%@taglib uri="/struts-tags" prefix="s"%> <%@page import="java.util.ArrayList"%> <%@page import="java.util.Iterator"%> <%@page import="net.roseindia.model.QuestionModel"%><html> <head> <title>Insert title here</title> </head> <body> <h1>Question List...........</h1> <s:form action="success.jsp"> <% int i=1; ArrayList<QuestionModel> questions = (ArrayList<QuestionModel>) request.getAttribute("questionList"); Iterator itr = questions.iterator(); while (itr.hasNext()) { QuestionModel obModel = (QuestionModel) itr.next(); %> <s:bean name="net.roseindia.model.QuestionModel" var="obQuesModel"> <s:param name="id"><%=i%></s:param> <s:param name="question"><%=obModel.getQuestion()%></s:param> </s:bean> <s:checkbox name="answer" label="%{#obQuesModel.question}" id="a" fieldValue="%{#obQuesModel.question}"></s:checkbox> <br /> <% i=i+1; } %> <s:submit></s:submit> </s:form> </body> </html>
7)QuestionModel.java:
package net.roseindia.model; import java.io.Serializable; public class QuestionModel implements Serializable { private String question; private int id; public String getQuestion() { return question; } public void setQuestion(String question) { this.question = question; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
8)success.jsp:
<html> <head> <title>Selected CheckBox </title> </head> <body> <h1>Check Box Value...........</h1><hr/> <% String[] checkboxs=request.getParameterValues("answer");%> <%for(int i=0; i<checkboxs.length;i++) {%> <%=checkboxs[i] %><br/> <%}%> </body> </html>
9)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.devMode" value="false" /> <package name="roseindia" extends="struts-default"> <action name="displayques" > <result name="success">/Questions.jsp</result> </action> <action name="questionaction" class="net.roseindia.action.QuestionAction"> <result name="input">/Questions.jsp</result> <result name="success">/Questionlist.jsp</result> </action> </package> </struts>