[JBoss Seam] - Re: seam-ui example: s:selectItems example is broken
by stephen.friedrich
Thanks a lot for looking into this, I really, really do appreciate this.
I prepared an example to clarify what I want to achieve.
There are departments which have employees.
In the edit page for a department I'd like to have a multi-select for the employees that work in the department.
(A selectManyShuffle form trinidad ultimately, for now I am testing with a standard selectManyListbox.)
I started with the seampay example and made the following additions.
Then directly opened http://127.0.0.1:8080/seam-pay/departments.seam
Currently edits never seem to make it to the DB.
My "hack" that manually sets the parent's id at the child entities when the list setter is called is no good either, because it does not remove deselected entities from the selection.
I played with different cascade options, but that also didn't make any difference.
Any idea what I need to change to make this work?
Added to import.sql
insert into Department(id, name) values(1, 'Accounting');
| insert into Department(id, name) values(2, 'Marketing');
| insert into Department(id, name) values(3, 'R&D');
|
| insert into Employee(id, department_id, name) values(1, 1, 'Bean Counter');
| insert into Employee(id, department_id, name) values(2, 1, 'Mister Money');
| insert into Employee(id, department_id, name) values(3, 1, 'Calc U. Lator');
|
| insert into Employee(id, department_id, name) values(4, 2, 'Power Pointy');
|
| insert into Employee(id, department_id, name) values(5, 3, 'Heinz Hacker');
| insert into Employee(id, department_id, name) values(6, 3, 'Sam Seam');
|
Added to components.xml
<framework:entity-query name="departments"
| ejbql="from Department"
| order="name"
| max-results="20"
| entity-manager="#{entityManager}" />
|
| <framework:entity-query name="employees"
| ejbql="from Employee"
| order="name"
| max-results="20"
| entity-manager="#{entityManager}" />
|
| <framework:entity-home name="departmentHome"
| entity-class="org.jboss.seam.example.seampay.Department"
| entity-manager="#{entityManager}" />
|
| <factory name="selectedDepartment" value="#{departmentHome.instance}"/>
|
New: departments.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
| <html xmlns="http://www.w3.org/1999/xhtml"
| xmlns:ui="http://java.sun.com/jsf/facelets"
| xmlns:h="http://java.sun.com/jsf/html"
| xmlns:f="http://java.sun.com/jsf/core"
| xmlns:s="http://jboss.com/products/seam/taglib"
| template="template.xhtml">
| <head>
| <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
| <title>Seam Pay</title>
| <link href="screen.css" rel="stylesheet" type="text/css" />
| <link href="date.css" rel="stylesheet" type="text/css" />
| </head>
| <body>
|
| <div class="body">
| <h1>Departments</h1>
|
| <h:messages styleClass="message"/>
|
|
| <!-- search results -->
| <table class="results">
| <tr>
| <th>Id</th>
| <th>Name</th>
| </tr>
| <ui:repeat value="#{departments.resultList}" var="department">
| <tr>
| <td>
| <s:link view="/departments.xhtml" value="#{department.id}">
| <f:param name="departmentId" value="#{department.id}"/>
| </s:link>
| </td>
| <td>#{department.name}</td>
| </tr>
| </ui:repeat>
| </table>
| <h:outputText value="No departments found" rendered="#{empty departments.resultList}" styleClass="message"/>
|
|
| <f:subview id="departmentDetails" rendered="#{departmentHome.idDefined}">
| <h2>Department Details</h2>
|
| <h:form>
| <s:validateAll>
| <table>
| <tr>
| <td>Name:</td>
| <td>
| <h:inputText id="name" value="#{selectedDepartment.name}" required="true"/>
| </td>
| </tr>
| </table>
|
| <h:selectManyListbox value="#{selectedDepartment.employees}" size="8">
| <s:selectItems value="#{employees.resultList}" var="employee" label="#{employee.name}" />
| <s:convertEntity />
| </h:selectManyListbox>
| </s:validateAll>
|
| <h:commandButton value="Update" action="#{departmentHome.update}" />
| </h:form>
| </f:subview>
|
| </div>
|
| </body>
| </html>
|
New: Department.java
package org.jboss.seam.example.seampay;
|
| import javax.persistence.*;
| import java.io.Serializable;
| import java.util.List;
|
| @Entity
| public class Department implements Serializable {
| @Id @GeneratedValue
| private Long id;
|
| String name;
|
| @OneToMany(mappedBy = "department", cascade = CascadeType.ALL)
| private List<Employee> employees;
|
| public Long getId() {
| return id;
| }
| public void setId(Long id) {
| this.id = id;
| }
|
| public String getName() {
| return name;
| }
|
| public void setName(String name) {
| this.name = name;
| }
|
| public List<Employee> getEmployees() {
| return employees;
| }
|
| public void setEmployees(List<Employee> employees) {
| this.employees = employees;
| }
| }
New: Employee.java
package org.jboss.seam.example.seampay;
|
| import javax.persistence.Entity;
| import javax.persistence.GeneratedValue;
| import javax.persistence.Id;
| import javax.persistence.ManyToOne;
| import java.io.Serializable;
|
| @Entity
| public class Employee implements Serializable {
| @Id @GeneratedValue
| private Long id;
|
| String name;
|
| @ManyToOne
| private Department department;
|
| public Long getId() {
| return id;
| }
| public void setId(Long id) {
| this.id = id;
| }
|
| public String getName() {
| return name;
| }
|
| public void setName(String name) {
| this.name = name;
| }
|
| public Department getDepartment() {
| return department;
| }
|
| public void setDepartment(Department department) {
| this.department = department;
| }
| }
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4069777#4069777
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4069777
17Â years, 3Â months
[JBoss Seam] - Re: File upload to application folder
by gmarcus
Seam 1.2.1GA
Jboss 4.0.5
I think the challenge here is not receiving the file, but then being able to write the file to a specified location.
Specifically, I wish to allow a user to upload a .jpg or .png and have it saved off my webroot so that the image can be displayed using an image tag. I do not want to save these images into the database as seamspace and ui does. I want to put them into the filesystem.
I currently have s:fileupload configured and working. The filecontents are available to my SFSB as
| private byte[] picture
|
Even if it was an InputStream, I still need to be able to write it out to a new file off of webroot.
Here is what I have so far:
profile.xhtml
| <s:fragment rendered="#{user.picture ne null}">
| <s:div id="memberPicture">
| <h:outputLabel for="picture">Member photo:</h:outputLabel>
| <h:outputText value="#{user.picture.originalFilename}" />
| <h:graphicImage value="/content/#{user.picture.filename}"/>
| </s:div>
| </s:fragment>
| <s:fragment rendered="#{identity.loggedIn and (loggedInUser.id==user.id)}">
| <s:fragment rendered="#{user.picture==null}">
| <h:form enctype="multipart/form-data">
| <s:validateAll>
| <div class="formRow">
| <h:outputLabel for="picture">Member photo</h:outputLabel>
| <s:fileUpload id="picture" data="#{profile.picture}" accept="images/png,images/jpg" contentType="#{profile.pictureContentType}" fileName="#{profile.pictureOriginalFilename}"/>
| <div class="validationError"><h:message for="picture"/></div>
| </div>
| </s:validateAll>
| <div class="buttons">
| <h:commandButton value="Upload" action="#{profile.addPicture}" />
| </div>
| </h:form>
| </s:fragment>
| <s:fragment rendered="#{user.picture ne null}">
| <h:form enctype="multipart/form-data">
| <div class="buttons">
| <h:commandButton value="Delete" action="#{profile.deletePicture}" />
| </div>
| </h:form>
| </s:fragment>
| </s:fragment>
|
ProfileAction.java (the SFSB)
| @Stateful
| @Name("profile")
| public class ProfileAction implements Profile
| {
| @In(required = false, create = false)
| @Out(required = false, scope=SESSION)
| private User loggedInUser;
|
| private String pictureOriginalFilename;
| private String pictureContentType;
| private byte[] picture;
| ...
|
| public String addPicture()
| {
| log.info("addPicture() called");
| log.info("addPicture() - picture size in bytes:#0", picture==null?"0":picture.length);
| log.info("addPicture() - original filename:#0", pictureOriginalFilename);
| log.info("addPicture() - contentType:#0", pictureContentType);
|
| User userToUpdate = em.find(User.class, loggedInUser.getId());
| if (picture != null && picture.length > 0)
| {
| log.info("addPicture() - about to save MemberImage");
|
| // need to save byte[] picture to a unique filename
| try
| {
| OutputStream pictureFileStream = new FileOutputStream(pictureOriginalFilename);
| pictureFileStream.write(picture);
| pictureFileStream.close();
| } catch (Exception e)
| {
| log.info("addPicture() - Problem saving picture: #0", e);
| }
| MemberImage img = new MemberImage();
| img.setMember(userToUpdate);
| img.setContentType(pictureContentType);
| img.setOriginalFilename(pictureOriginalFilename);
| img.setFilename(pictureOriginalFilename);
| em.persist(img);
| log.info("addPicture() - saved MemberImage:#0", img);
|
| userToUpdate.setPicture(img);
| em.persist(userToUpdate);
|
| loggedInUser = userToUpdate;
| }
|
| return "success";
| }
|
| ...
|
| }
|
How do I write the file out to my webroot? Specifically, I want to write them into /content and be able to use s:graphicImage to display it. Any code examples would be very much appreciated.
Glenn
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4069769#4069769
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4069769
17Â years, 3Â months