0

I am doing this in JSF 2.0. I have implemented add and view pages with same controller. I don't know if it's best practise to use same controller, is it? As in this example it uses single page for all add,edit, view but I have different pages. So when migrating from view page to edit page I want to preserve value but I can't. How to preserve value between different pages in same controller? The output consoles shows the value of edit changes from true to false which I change to true in editLegendType function.

@ManagedBean
@ViewScoped
public class LegendController implements Serializable {

    LegendDTO legendDTO = new LegendDTO();
    String selectedLegend;
boolean edit;

public LegendController() {
      Logger.getLogger(LegendController.class.getName()).warning("The size of list" + edit);
    if (!edit) {
        legendDTO.getList().add(new Legend());
        Logger.getLogger(LegendController.class.getName()).warning("The size of list" + legendDTO.getList().size());
    }
}


//All function from here is to legend edit
public String editLegendType(LegendDTO dto) {
    edit = true;
    legendDTO = dto;
        Logger.getLogger(LegendController.class.getName()).warning("The size of list" + edit);
        return "addLegend";//from view page to addPage for edit.
    }
}
Community
  • 1
  • 1
kinkajou
  • 3,664
  • 25
  • 75
  • 128

1 Answers1

1

Using the same controller for multiple Views is OK IMHO, if it prevents code duplication and the usability is improved by using separate views.

Unfortunately, you cannot continue using the View scope. There are several alternatives though. You could either use the new custom Conversation scope, or fall back to Session scope. Both have pros and cons - with the conversation scope you will have to do the scope handling yourself. With the Session scope, you may unnecessarily put too much data in the session.

So if I had to choose, I would rather use the conversation scope over session scope as the more tedious but more clean solution.

EDIT: Please note that the conversation scope is not a JSF feature, it comes from CDI, meaning that you would have to change the annotation on your bean from @ManagedBean to @Named

EDIT2: To use CDI on tomcat, you need to have it in your classpath. If you are using maven, add this to your .pom, otherwide, download and use the jar "manually".

<dependency>
  <groupId>org.jboss.weld.servlet</groupId>
   <artifactId>weld-servlet</artifactId>
   <version>1.1.9-Final</version>
</dependency>

Additionally, you would have to add this to your web.xml:

<listener>
   <listener-class>
      org.jboss.weld.environment.servlet.Listener
   </listener-class>
</listener>

You may also need an empty beans.xml. Im not sure about that though.

Community
  • 1
  • 1
kostja
  • 60,521
  • 48
  • 179
  • 224
  • import javax.enterprise.context.Conversation; import javax.enterprise.context.ConversationScoped; import javax.inject.Inject; import javax.inject.Named; these package are not available should I need to do somthing more. I am using tomcat 7. – kinkajou Aug 08 '12 at 14:23
  • @Kitex have added CDI-on-tomcat detials to the answer – kostja Aug 08 '12 at 14:37