Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form action="addAnimal" method="post">
- <fieldset>
- <legend>What is Your Favorite Pet?</legend>
- <input type="checkbox" name="animal" value="Cat"/>Cats <br />
- <input type="checkbox" name="animal" value="Dog" />Dogs<br />
- <input type="checkbox" name="animal" value="Bird" />Birds<br />
- <input type="submit" value="Submit now" />
- </fieldset>
- </form>
- package com.bean;
- import com.dao.AnimalDao;
- import com.opensymphony.xwork2.ActionSupport;
- public class Animal extends ActionSupport {
- private String animal;
- public String getAnimal() {
- return animal;
- }
- public void setAnimal(String animal) {
- this.animal = animal;
- }
- public String execute(){
- AnimalDao.addAnimal(this);
- return SUCCESS;
- }
- }
- import java.util.Map;
- import org.apache.struts2.dispatcher.SessionMap;
- import org.apache.struts2.interceptor.SessionAware;
- import com.bean.Animal;
- import com.connection.DatabaseConnection;
- import com.opensymphony.xwork2.ActionSupport;
- public class AnimalDao extends ActionSupport implements SessionAware {
- SessionMap<String,Object> map;
- public static void addAnimal(Animal animal) {
- // TODO Auto-generated method stub
- try{
- Connection con=DatabaseConnection.Dbconnection();
- String sql="insert into chk values(?)";
- PreparedStatement ps=con.prepareStatement(sql);
- ps.setString(1,animal.getAnimal());
- ps.executeUpdate();
- }catch(Exception e){
- }
- }
- public String retriveAnimal(){
- ArrayList<Animal> aList=new ArrayList<Animal>();
- try{
- Connection con=DatabaseConnection.Dbconnection();
- String sql="select * from chk";
- PreparedStatement ps=con.prepareStatement(sql);
- ResultSet rs=ps.executeQuery();
- while(rs.next()){
- Animal a=new Animal();
- a.setAnimal(rs.getString(1));
- aList.add(a);
- map.put("aList",aList);
- }
- }catch(Exception e){
- System.out.println(e);
- }
- return SUCCESS;
- }
- @Override
- public void setSession(Map<String, Object> map) {
- // TODO Auto-generated method stub
- this.map=(SessionMap<String, Object>) map;
- }
- }
- <action name="addAnimal" class="com.bean.Animal">
- <result name="success">index.jsp</result>
- </action>
- <action name="chkbox" class="com.dao.AnimalDao" method="retriveAnimal">
- <result name="success">viewuser.jsp</result>
- </action>
- <s:iterator value="#session.aList" var="stat">
- <s:checkbox name="animal[%{#stat.index}]" value=""/>
- </s:iterator>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement