I have such struts-config.xml:
<?xml version="1.0" encoding="UTF-8" ?>

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

<struts-config>
    <form-beans>
        <form-bean name="AddActionBean" type="ru.add.AddActionBean"/>
        <form-bean name="DeleteActionBean" type="ru.delete.DeleteActionBean"/>
    </form-beans>
    
    <global-exceptions>
    </global-exceptions>

    <global-forwards>
    </global-forwards>

    <action-mappings>
        <action input="/add.jsp" name="AddActionBean" path="/addPerson" 
scope="request" type="ru.add.AddPersonAction">
            <forward name="success" path="/list.jsp"/>
        </action>
        <action input="/list.jsp" name="DeleteActionBean" path="/deletePerson" 
scope="request" type="ru.delete.DeletePersonAction">
            <forward name="successDelete" path="/listResult.jsp"/>
        </action>
    </action-mappings>

   ...
  
</struts-config>

File DeleteActionBean.java:
package ru.delete;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import java.sql.*;
import javax.sql.*;
import java.util.*;
import javax.naming.*;
import ru.database.DataBaseHandler;

public class DeleteActionBean extends org.apache.struts.action.ActionForm {
    
    private String id;
    private String name;
    private String email;
    private String gender;
    private ResultSet personsList;
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getEmail() {
        return email;
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
    
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public String getGender() {
        return gender;
    }
    
    public void setGender(String gender) {
        this.gender = gender;
    }
    
    public ArrayList getPersonsList() throws SQLException {
        ArrayList list = new ArrayList();
        
        while(personsList.next()) {
            int id = personsList.getInt("id");
            String name = personsList.getString("name");
            String email = personsList.getString("email");
            boolean gender = personsList.getBoolean("sex");
            
            list.add(new PersonEntry(id, name, email, gender));
        }
        
        return list;
    }
    
    public DeleteActionBean() throws NamingException, SQLException {
        super();
        DataBaseHandler db = new 
DataBaseHandler("java:/comp/env/jdbc/postgres");
        personsList = (ResultSet) db.returnQuery("SELECT * FROM persons");
    }
    
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest 
request) {
        ActionErrors errors = new ActionErrors();
        if (getName() == null || getName().length() < 1) {
            errors.add("name", new ActionMessage("error.name.required"));
        }
        return errors;
    }
}

File DeletePersonAction.java:
package ru.delete;

import java.util.*;
import java.io.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

import ru.database.DataBaseHandler;

public class DeletePersonAction extends Action {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "successDelete";
    
    public DeletePersonAction() throws IOException {
// Test code
        PrintWriter out = new PrintWriter(new FileWriter("c:/hello.txt"));
        out.print(String.valueOf(new Random().nextInt()));
        out.close();     
// End of test code
    }    
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

// Test code        
        PrintWriter out = new PrintWriter(new FileWriter("c:/hello.txt"));
        out.print(String.valueOf(new Random().nextInt()));
        out.close();
 // End of test code
       
        Iterator it = request.getParameterMap().entrySet().iterator();
        
        DataBaseHandler db = new 
DataBaseHandler("java:/comp/env/jdbc/postgres");
        db.voidQuery("delete from persons where id=" + it.next());
        db.close();

        return mapping.findForward(SUCCESS);
        
    }
}

File list.jsp:
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"; prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"; prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"; prefix="logic" %>

<html:html>
    <head>
        <title>List</title>
    </head>
    <body>
        <html:form action="/deletePerson" method="POST">
            <table border="1">
                <tr>
                    <td>Name</td>
                    <td>E-mail</td>
                    <td>Gender</td>
                    <td>Action</td>
                </tr>
                <logic:iterate id="element" name="DeleteActionBean" 
property="personsList">
                    <tr>
                        <!-- Name of person -->
                        <td><bean:write name="element" property="name"/></td>
                        
                        <!-- E-mail of person -->
                        <td><bean:write name="element" property="email"/></td>
                        
                        <!-- Gender of person -->
                        <td><bean:write name="element" property="gender"/></td>
 
                        <!-- Delete button -->
                        <bean:define id="tempId" name="element" property="id"/>
                        <td><html:submit property="tempId" value="Delete"/></td>
                    </tr>
                </logic:iterate>
            </table>
        </html:form>
    </body>
</html:html>

Then i press button 'Delete', so there is no action and i'm on page 
deletePerson.do
(and i my saw list.jsp again, without any changes), but DeletePersonAction.java 
doesn't
work (i understood this with test code). When i'm press button delete don't 
show me listResult.jsp,
but i declared this in struts-config.xml. What's the problem? Why actions 
declared in DeletePersonAction.java
don't play any role? Thanx.

-- 
by SkyStar
Without music, life would be a mistake. (Friedrich Nietzsche)


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to