# ActionForm erstellen?



## 23 (15. Mai 2009)

Hey,


```
public class CategoryDTO implements Serializable {

    private String id;

    private String name;

    private String description;

    private String picture;

    public CategoryDTO(ResultSet rs) {

        try {

            id = rs.getString("categoryid");

            name = rs.getString("name");

            description = rs.getString("description");
            
            picture = rs.getString("imageurl");

        } catch (SQLException e) {

            e.printStackTrace();

        }
        
    }

    public void setId(String id) {
    
        this.id = id;
        
    }

    public String getId() {
    
        return id;
        
    }

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

    public String getName() {
    
        return name;
        
    }
    
    public void setDescription(String description) {
    
        this.description = description;
        
    }

    public String getDescription() {
    
        return description;
        
    }

    public void setPicture(String picture) {
    
        this.picture = picture;
        
    }

    public String getPicture() {
    
        return picture;
        
    }

}
```


```
public class CategoryDB {

    public static Collection<CategoryDTO> findAll(Connection con) {
        
        List<CategoryDTO> li = new ArrayList<CategoryDTO>();

        if(con != null) {
           
            try {
            
                PreparedStatement ps = con.prepareStatement(
                "select * from category");
                
                ResultSet rs = ps.executeQuery();
                
                while(rs.next() == true) {
                
                    li.add(new CategoryDTO(rs));
                
                }
                
                rs.close();
                
                ps.close();
                
            } catch (SQLException e) {
            
                e.printStackTrace();
            
            } 
            
        } else 
            throw new IllegalArgumentException();
        
        return li;
        
    }

}
```

Nun möchte ich eine Action schreiben die eine ActionForm nutz und auf einer JSP die Categories in einer SELECTBOX (Cateory name) ausgibt. Was muss ich tun?

Danke schonmal!


----------



## HLX (15. Mai 2009)

ActionForm und Action? Das klingt nach Struts:

1. Klasse von ActionForm ableiten und Liste mit Werten sowie selektiertem Wert als Variable anlegen
2. Klasse von Action ableiten, ActionForm in der Methode execute(...) befüllen und an die JSP-Seite weiterleiten
3. ActionForm und Action in Struts-Config eintragen
4. JSP-Seite schreiben, Werte aus ActionForm in SelectBox auslesen.


----------



## 23 (17. Mai 2009)

Danke ich habs hinbekommen 

Mein Prof hat Struts auf 3 Folien erklärt...


----------



## 23 (21. Mai 2009)

Nun habe ich folgendes Problem:


```
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>   
<title>Petstore - Item Edit</title>
<link rel="stylesheet" type="text/css" href="style/style.css">
</head>

<body>
<div id="header">
  <div align="center" class="headerfont">The Petstore!</div>
</div>

<div id="content"><span class="contenthead">ItemId: ${contoller_item_edit_id.id}</span><br><br>


<html:form action="/item_edit">


     Name<br>
    <html:text property="name"/><br>
    
    Price<br>
    <html:text property="price"/><br>
    
    Thumburl<br>
    <html:text property="thumb"/><br>

</html:form>


<!-- alt  
<c:if test="${!empty contoller_item_edit_id}">

<div id="content"><span class="contenthead">ItemId: ${contoller_item_edit_id.id}</span><br><br>

<img src="${contoller_item_edit_id.picture}"><br><br>

<form name="item_edit_form" method="post" action="viewitems">
  Name<br>
  <input type="text" value="${contoller_item_edit_id.name}" name="item_edit_name"><br><br>
    Price<br>
  <input type="text" value="${contoller_item_edit_id.price}" name="item_edit_price"><br><br>
    Thumburl<br>
  <input type="text" value="${contoller_item_edit_id.thumb}" name="item_edit_thumb">
  
  <input name="item_edit_id" type="hidden" value="${contoller_item_edit_id.id}">
  
  <br><br><input type="submit" name="item_edit_submit" value="Save" class="pgbutton">
</form>

</c:if>
alt -->  



<br>

<html:link page="/item_uebersicht.do"><bean:message key="link.item_show_back"/></html:link>

</div>
```

Ich übergebe dieser item_edit.jsp eine ActionForm aber kann ich auch außerhalb dieses <html:form Tags auf die Werte zugreifen?

Wie?


```
public class PetEditForm extends ActionForm {

    private String id;

    private String name;

    private String price;

    private String thumb;

    private String picture;
    
    private String description;

    public void setId(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getPrice() {
        return price;
    }

    public void setThumb(String thumb) {
        this.thumb = thumb;
    }

    public String getThumb() {
        return thumb;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }

    public String getPicture() {
        return picture;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

}
```


----------

