Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6032591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:21:23+00:00 2026-05-23T05:21:23+00:00

I had a problem, which I solved, but I feel like my solution is

  • 0

I had a problem, which I solved, but I feel like my solution is a bad hack. Is there a better way?

I have a page, on which I placed the form, which shows properties of some object, as in example (obvious details omitted).

Ticket.java:

@Entity
public class Ticket {
    @Id
    private Long id;
    private String title;
    private byte priority;
    // Getters, setters...
}

TicketController.java

@RequestScoped
public class TicketController {
    private Ticket ticket = new Ticket();
    // Getters, setters...

    public String doUpdateTicket() {
        Ticket t = ticketEJB.getTicketById(ticket.getId());
        t.setTitle(ticket.getTitle());
        t.setPriority(ticket.getPriority());
        ticketEJB.updateTicket(t);
        ticket = t;
        return "view.faces";
    }
}

edit.xhtml (just the form, everything else is boilerplate)

<h:form>
    <h:inputHidden value="#{ticketController.ticket.id}" />
    <h:panelGrid columns="2">
        <h:outputLabel value="ID"/>
        <h:outputLabel value="#{ticketController.ticket.id}"/>
        <h:outputLabel value="Title: "/>
        <h:inputText value="#{ticketController.ticket.title}"/>
        <h:outputLabel value="Priority: "/>
        <h:inputText value="#{ticketController.ticket.priority}" />
        <h:commandButton value="Submit" 
             action="#{ticketController.doUpdateTicket}" />
    </h:panelGrid>
</h:form>

Also there is TicketEJB, which is responsible for fetching those tickets, persisting, etc.

So I create a hidden input in the form, then (in managed bean) I find ticket, using provided id, then manually copy all the fields from ticket object of managed bean to the fetched ticket, then persist it… It involves the violation of DRY principle (I already stumbled on a bug when I added a field to Ticket, but forgot to copy it in the doUpdateTicket().

So, maybe there is a better way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:21:24+00:00Added an answer on May 23, 2026 at 5:21 am

    Just get the original ticket from the EJB during preRenderView of a view scoped bean instead of creating a new one yourself. Assuming that the ticket ID is been passed as a request parameter with name id:

    edit.xhtml

    <f:metadata>
        <f:viewParam name="id" value="#{ticketController.id}" />
        <f:event type="preRenderView" listener="#{ticketController.preLoad}" />
    </f:metadata>
    ...
    

    TicketController

    @ManagedBean
    @ViewScoped
    public class TicketController {
        private Long id;
        private Ticket ticket;
    
        @EJB
        private TicketEJB ticketEJB;
    
        public void preLoad() {
            ticket = ticketEJB.getTicketById(id);
        }
    
        public String doUpdateTicket() {
            ticketEJB.updateTicket(ticket);
            return "view.faces";
        }
    
        // ...
    }
    

    The only difference is that the input fields don’t blank out. But isn’t that just the whole idea behind an “edit” form? That issue is then also immediately fixed that way.

    Oh and your

    <h:outputLabel value="#{ticketController.ticket.id}"/>
    

    really needs to be a

    <h:outputText value="#{ticketController.ticket.id}"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a problem this week (which thankfully I've solved in a much better
I've had this problem a few times and solved it the same way. But
Not too long ago, I had a problem which required me to set WinDbg.exe
I had a problem in site map path I had navigation bar which take
We had huge performance problem when deploying our ASP.NET app at a customer, which
One thing with which I have long had problems, within the CakePHP framework, is
I had a problem that was partially solved. To explain it quickly : I
I have been on SO for awhile now trying to get this problem solved
I have a webpage which I would like users to be able to send
I'm facing a problem I have never had before. The other day when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.