如何从Struts 2中动态生成的JSP页面获取数据?

时间:2014-11-30 09:24:15

标签: java jsp struts2 jsp-tags

我使用Struts2生成以下页面。它正在正常生成。

我的问题是当我点击delete按钮时如何从生成的页面中获取值,以便我可以进一步处理从数据库中删除数据并使用剩余数据重新生成页面?

  <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
 <%@ taglib uri="/struts-tags" prefix="s" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h3>All Records:</h3>
        <table>  
        <s:form action="upload"> 
        <s:iterator   value="list"> 
        <tr>
        <td ><s:property value="id"/></td> 
        <td ><s:property value="name"/></td>  
        <td><s:property value="password"/></td>  
        <td><s:property value="email"/></td> 
        <td><s:property  value="gender"/></td>
        <td><s:checkbox  name="checked" label="isChecked"  theme="simple" /></td>       
        </tr>    
        </s:iterator> 
        <s:submit value="delete" name="delete" />
        </s:form>
        </table>
</body>
</html>

RegisterAction.java

package com.javatpoint;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class RegisterAction {
private String name,password,email,gender,country;
int id;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getEmail() {
    return email;
}

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

public String getGender() {
    return gender;
}

public void setGender(String gender) {
    this.gender = gender;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

ArrayList<User> list=new ArrayList<User>();  

public ArrayList<User> getList() {  
    return list;  
}  
public void setList(ArrayList<User> list) {  
    this.list = list;  
}  

public String execute(){
    int i=RegisterDao.save(this);
    if(i>0){
        Connection con=RegisterDao.con;
        try {
             PreparedStatement ps=con.prepareStatement("select * from STRUTSUSER");
             ResultSet rs=ps.executeQuery();  
        //  rs = ps.getGeneratedKeys();
              while(rs.next()){  
               User user=new User();  
               user.setId(rs.getInt(1));

               user.setName(rs.getString(1));  
               user.setPassword(rs.getString(2));  
               user.setEmail(rs.getString(3)); 
               user.setGender(rs.getString(4));
               list.add(user);  
              // System.out.println("yo");
              }  

              con.close();  
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
    return "success";
    }
    return "error";
}

1 个答案:

答案 0 :(得分:1)

您可以尝试以下内容:

  public class RegisterAction {
   ....
    public String execute(){
    ...
    }

    public List<User> getAllUsers() {
    List<User> users = new ArrayList<User>();
    Connection con=RegisterDao.con;
    try
    {
        Statement statement = conn.createStatement();
        ResultSet rs = statement.executeQuery("select * from STRUTSUSER");
        while (rs.next())
        {
            User user = new User();
            user.setID(rs.getInt("id"));
            user.setName(rs.getString("name"));
            user.setPassword(rs.getString("pass"));
            user.setEmailID(rs.getString("emailid"));
            users.add(user);
        }
    } 
    catch (SQLException e)
    {
        e.printStackTrace();
    }
    return users;
   }

    public String delete() {
      HttpServletRequest request ServletActionContext.getRequest();
      int userId = request.getParameter("id");
      deleteUser(userId);
      return SUCCESS;
     }

    private void deleteUser(int userId)
        {
            Connection con=RegisterDao.con;
            try
            {
               PreparedStatement ps = conn.prepareStatement("delete from STRUTSUSER where id=?");
                // Parameters start with 1
                ps.setInt(1, userId);
                ps.executeUpdate();

            } catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
     }

JSP页面:view.jsp

  <s:iterator value="list">
   ...
 </s:iterator>
 <s:hidden name="id" value="%{#list.id}" />
 <s:submit value="delete" name ="delete" action="deleteUserAction"/>

在Struts.xml中

 <action name="deleteUserAction" class="example.RegisterAction" method="delete">
       <result name="success">view.jsp</result>
 </action>

删除用户后,您可以从操作中调用getAllUsers()

希望这有助于