Multiple outcomes in implicit navigation with JSF 2.0 - jsf-2

Please I am missing a point here. How can I handle a request with multiple outcomes in JSF 2.0 using the implicit navigation
For example using the explicit navigation I can write the following:
<navigation-rule>
<from-view-id>/products/DeleteItem.xhtml</from-view-id>
<navigation-case>
<from-outcome>itemDeleted</from-outcome>
<to-view-id>/products/Success.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>itemNotExist</from-outcome>
<to-view-id>/products/Failure.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
However How can I achieve the same thing with the implicit navigation, I have tried the following :
<h:link value="Delete a Product" outcome="Success" />
only one case is mentioned but I want to forward either to Success.xhtml or Failure.xhtml depending on the outcome.
Here a some additional information for the implicit navigation.
Managed bean:
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
#ManagedBean
#RequestScoped
public class DeletionBean {
#EJB
private app.session.ProductFacade ejbProductFacade;
private int id=0;
public DeletionBean() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String deleteProduct(){
String result;
result=ejbProductFacade.deleteItem(id);
if(result.equals("success"))
{
status="Successfully performed"; //msg to be shown
return "product/DeleteItem";
}
else return "product/Failure";
}
}
Form:
<h:form>
<table border="1" width="5" cellspacing="1" cellpadding="1">
<tr>
<td> Product ID </td>
<td>
<h:inputText id="pid" size="10" value="#{deletionBean.id}" title="Product ID" required="true" requiredMessage="Product ID required"/>
</td>
</tr>
<tr>
<td colspan="2">
<h:commandLink value="Delete" action="#{deletionBean.deleteProduct}" />
</td>
</tr>
</table>
</h:form>
Current error message:
Unable to find matching navigation case with from-view-id '/product/DeleteItem.xhtml' for action '#{deletionBean.deleteProduct}' with outcome 'product/Failure'

Supposing you'll perform an action method to delete your product, you should make your method return the desired outcome. Using JSF 2, there's no need of using outcome ids anymore, even you can declare them in your faces-config.xml file, JSF ties a provided outcome with a specific page:
<h:commandLink action="#{bean.deleteProduct(product)}"
value="Delete product" />
public String deleteProduct(Product p){
//Try to delete the product and return "products/xxx",
//which will be converted to "/contextname/products/xxx.xhtml"
try{
daoService.delete(p);
return "/products/Success";
catch(Exception e){
return "/products/Failure";
}
}
That will POST the server and behaves like an HTML form submit button (that's why it's called commandLink). Nevertheless if you want just to perform a GET to an specific view, you can use its view id in the outcome:
<h:link value="Go to success doing nothing"
outcome="/products/Success" />
See also:
Implicit navigation in JSF 2

Related

update component in another xhtml form from Custom layout p:selectOneRadio not working

My xhtml is split in to Menu area (defaultMenu.xhtml) and Content area (defaultContent.xhtml).
The code for defaultMenu.xhtml is:
<h:form id="defaultmenuform">
<p:outputPanel id="menupanel" class="contain auto-fixed-center">
<p:panel id="pmenu" visible="#{phController.user.menuVisible}">
<table id="stutable">
<tr>
<td width="15%">
<p:outputLabel id="stuname" value="#{phController.phBean.studentName}" />
</td>
<td>
<p:tabMenu activeIndex="#{param.selectedtab}">
<p:menuitem value="Home" outcome="phome" icon="ui-icon-star">
<f:param name="selectedtab" value="0" />
</p:menuitem>
<p:menuitem value="Bank" outcome="bhome" icon="ui-icon-person">
<f:param name="selectedtab" value="1" />
</p:menuitem>
</p:tabMenu>
</td>
</tr>
</table>
</p:panel>
</p:outputPanel>
</h:form>
The defaultContent.xhtml actually displays the ph.xhtml content (as part of functional navigation) and the code is:
<ui:define name="content">
<f:event listener="#{phController.readPeople}" type="preRenderView">
</f:event>
<h:form id="form">
<p:selectOneRadio id="selstud" value="#{phController.phBean.ssSeq}" layout="custom">
<p:ajax update=":defaultmenuform:parentmenupanel :defaultmenuform:stuname" listener="#{phController.onChangePerson}"/>
<f:selectItems value="#{phController.selectStudents}" />
</p:selectOneRadio>
<div style="width: 300px; float:left;">
<p:dataGrid var="studentlist" value="#{phController.listStudent}" columns="1" rowIndexVar="stuindex">
<p:panel header="" style="text-align:left">
<h:panelGrid columns="1" style="width:100%">
<h:outputText value="#{studentlist.studentName}" />
<p:radioButton for=":form:selstud" itemIndex="#{stuindex}"/> Select
</h:panelGrid>
</p:panel>
</p:dataGrid>
</div>
</h:form>
</ui:define>
The code for backing bean is:
Map<String, Object> studentparam = new HashMap<>();
studentparam.put("studentSeq", phBean.getSsSeq());
lS = getBaseDAOService().readStudent("readStudent", studentparam);
phBean.setStudentName(lS.get(0).getStudentFirstName() + " " + lS.get(0).getStudentLastName());
As you can see, I am calling the onChangeStu method to display the selected Student Name in defaultMenu.xhtml. I am using Custom Layout p:selectOneRadio in ph.xhtml and onClick trying to update a p:outputLabel in defaultMenu.xhtml.
The backing bean method gets invoked successfully and the value is also set in variable phController.phBean.studentName, but the update is not working. I also checked using view source and the id is “:defaultmenuform:stuname”, I also tried updating the menu panel ":defaultmenuform:menupanel”, but none of this works.
Not sure how to resolve this. Please suggest.
Including the structure of all .xhtmls
<h:body id="entirePageBody">
<div id="page">
<ui:insert name="header" >
<ui:include src="/template/defaultHeader.xhtml" />
</ui:insert>
<ui:insert name="menu" >
<ui:include src="/template/defaultMenu.xhtml" />
</ui:insert>
<div id="content_div" class="auto-fixed-center">
<div id="content_div_padding" class="content-block">
<ui:insert name="content" >
<ui:include src="/template/defaultContent.xhtml" />
<ui:debug hotkey="z" />
</ui:insert>
</div>
</div>
<ui:insert name="footer" >
<ui:include src="/template/defaultFooter.xhtml" />
</ui:insert>
</div>
</h:body>
PhController.java:
public class PhController extends BaseController implements Serializable {
private List<Stud> listStudent;
private List selectStudents;
SelectItem option;
private PhBean phBean;
private Boolean menuVisible;
int counter = 0;
public PhController() {
phBean = new PhBean();
}
public void readPeople() {
listStudent = new ArrayList<Stud>();
listStudent.add(new Stud(1, "John Miller"));
listStudent.add(new Stud(2, "Scott Jackson"));
selectStudents = new ArrayList();
option = new SelectItem(listStudent.get(0).getStudentSeq(), "Select");
selectStudents.add(option);
option = new SelectItem(listStudent.get(1).getStudentSeq(), "Select");
selectStudents.add(option);
phBean.setSsSeq(String.valueOf(1));
phBean.setSelectedName(listStudent.get(0).getStudentName());
menuVisible = true;
}
public void onChangePerson() {
phBean.setSelectedName(listStudent.get(1).getStudentName());
}
// Getters and Setters
}
PhBean.java:
public class PhBean implements Serializable {
private String ssSeq;
private String studName; // Used to display the name in the Menu bar.
private String selectedName;
public PhBean() {
}
// Getters and Setters
}
I'd say that in the p:ajax in defaultContent.xhtml the list of components to be updated should be separated with spaces only, no commas - so try changing this:
update=":defaultmenuform:menupanel, :defaultmenuform:stuname"
to this:
update=":defaultmenuform:menupanel :defaultmenuform:stuname"
UPDATE
I played with this a bit more and may have found a clue - please add the following code to defaultmenuform:
<p:messages autoUpdate="true" showDetail="true" />
This should help us tracking the reason for failed validation (in case failing validation is the root cause for you - as I said, I have rather limited possibility to reproduce this issue).
Anyway, when I selected some item in p:selectOneRadio, an error message like this appeared:
Conversion Error setting value 'test001.Student#4110c95c' for 'null Converter'.
And the root cause was on this row:
<p:selectOneRadio id="selstud" value="#{phController.phBean.ssSeq}" layout="custom">
p:selectOneRadio expects only String to be passed as a value - and ssSeq is very likely of a different type. Try to change the way value is populated to ensure it is always String - maybe a different attribute of the phBean or simply a brand new String one.
NOTE: if this doesn't help, maybe you could update your question with very simplified example of how phController and phBean could look like if we are to test it.
UPDATE #2
So you have explained there is a problem that you want to call phController.readPeople every time the page is loaded/refreshed, but instead it gets loaded with each and every Ajax request, thus overwriting the values.
In your PhController (it is a bean, right? session scoped?) you could add something like this (omitted null checks for the sake of readability):
public void readPeopleOnGet() {
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
HttpServletRequest req = (HttpServletRequest) ec.getRequest();
String reqMethod = req.getMethod();
if ("GET".equals(reqMethod)) {
readPeople();
}
}
With the above method you could keep this part of your defaultContext.xhtml in place, provided it is actually called (I assume so), just with the listener method changed:
<f:event listener="#{phController.readPeopleOnGet}" type="preRenderView">
</f:event>
The method readPeopleOnGet will still be called with every request to the page, but since Ajax requests are POST, it will only call readPeople when the page is loaded or refreshed as whole.
This may not be a "perfectly clean" solution, but seemed to work properly when I tested it.
UPDATE #3
Well, since you use PrimeFaces, it would be possible to identify Ajax call also this way:
public void readPeopleOnGet() {
RequestContext rc = RequestContext.getCurrentInstance();
if (!rc.isAjaxRequest()) {
readPeople();
}
}
But if I got your point from latest comments, you want to only run readPeople when the page is loaded for the very first time - so the following part could be even better for that.
You didn't answer if PhController is actually a bean, but I assume it is (there were no annotations visible from the code you posted). You may try making it #SessionScoped:
#ManagedBean
#SessionScoped
public class PhController extends BaseController implements Serializable {
// the rest of the PhController goes here...
Then you could add something like this to the PhController:
#PostConstruct
private void init() {
readPeople();
}
The annotation #PostConstruct ensures the method init is called exactly once after the bean was created. You can continue calling the method readPeople from other places as necessary, while removing both the <f:event ... /> and readPeopleOnGet as these will no longer be needed.

Dynamic adding text fields in JSF [duplicate]

This question already has answers here:
How to dynamically add JSF components
(3 answers)
Closed 7 years ago.
I have a screen with inputText, beside it there's a (+) button, when the user should press that button the form should add another extra inputText beside it (or below, whatever)
Here's the code:
<table>
<tr>
<td>
<p:inputText value="#{controller.x}" />
<img src="../images/ico_plus.png" />
</td>
</tr>
</table>
in Controller.java:
private String x;
public String getX(){return x}
public void setX(String val){x = val}
I need the page to be populated with multiple fields and the controller to have all the fields values' fetched
This question was answered more than one time, basically you need to keep a List for all fields in the bean, and you remove or add to this List with your buttons. Note this is important to be in ViewScoped or SessionScoped ortherwise your List will be reset at every actions.
View :
<h:form>
<h:dataTable id="tblFields" value="#{bean.fields}" var="field">
<h:column>
<h:inputText value="#{field.value}" />
</h:column>
<h:column>
<h:commandButton value="Remove">
<f:ajax listener="#{bean.onButtonRemoveFieldClick(field)}" immediate="true" render="#form" />
</h:commandButton>
</h:column>
</h:dataTable>
<h:commandButton value="Add">
<f:ajax listener="#{bean.onButtonAddFieldClick}" execute="#form" render="tblFields" />
</h:commandButton>
</h:form>
Helper class :
public class Field implements Serializable
{
private String m_sName;
public void setName(String p_sName)
{
m_sName = p_sName;
}
public String getName()
{
return m_sName;
}
}
Bean :
#ManagedBean
#ViewScoped
public class Bean implements Serializable
{
private List<Field> m_lFields;
public Bean()
{
m_lFields = new ArrayList();
m_lFields.add(new Field());
}
public void setFields(List<Field> p_lFields)
{
m_lFields = p_lFields;
}
public List<Field> getFields()
{
return m_lFields;
}
public void onButtonRemoveFieldClick(final Field p_oField)
{
m_lFields.remove(p_oField);
}
public void onButtonAddFieldClick(AjaxBehaviorEvent p_oEvent)
{
m_lFields.add(new Field());
}
}

Trouble with lifecycle in JSF 2.0 and selectManyListbox

I am trying to make a simple admin page for managing users list. The jsf code looks like this:
<h:selectOneMenu id="selectUser" value="#{adminBean.user_id}" valueChangeListener="#{adminBean.userSelected}" >
<f:selectItems value="#{adminBean.myModelUsersValues}" />
<a4j:ajax event="valueChange" render="login password privilege_list" execute="#this"/>
</h:selectOneMenu >
<table>
<tr>
<td><h:outputLabel styleClass="LabelStyle" value="login: "/></td>
</tr>
<tr>
<td>
<h:inputText id="login" value="#{adminBean.login}"/>
</td>
<td>
<h:message for="login" style="color:red"/>
</td>
</tr>
<tr>
<td><h:outputLabel styleClass="LabelStyle" value="password: "/></td>
</tr>
<tr>
<td>
<h:inputText id="password" value="#{adminBean.password}"/>
</td>
<td>
<h:message for="password" style="color:red"/>
</td>
</tr>
<tr>
<td><h:outputLabel styleClass="LabelStyle" value="privilege list: "/></td>
</tr>
<tr>
<td>
<h:selectManyListbox id="privilege_list" value="#{adminBean.privilegeList}">
<f:selectItems value="#{adminBean.privilegeValues}" />
</h:selectManyListbox >
</td>
<td>
<h:message for="privilege_list" style="color:red"/>
</td>
</tr>
</table>
<br/>
<h:commandButton id="addButton" value="Add" action="#{adminBean.addUser}" styleClass="ButtonStyle"/>
<h:commandButton id="deleteButton" value="Delete" action="#{adminBean.deleteUser}" styleClass="ButtonStyle"/>
<h:commandButton id="clearButton" value="Clear" action="#{adminBean.clear}" styleClass="ButtonStyle"/>
The problem is that when the page loads, all the items are empty. Now When I click on 'add' button I have discovered that the valueChangeListener="#{adminBean.userSelected}" runs, which replaces my privilege list with the ones from the first user. The same is when I use the clear button - all fields are empty, but when I click on the add button again, the list is the one from the first user (and only the list - no other input texts). I tried adding immediate="true" to the add button and that solves this problem, but off course then all the values I put into input text are not passed through to the adminBean.addUser action method. My bean is viewscoped (I needed to use it because of the validation error on selectManyListBox). Here is the Java code (the addUser method so far only sends a logger method and checks i login exists, and if sth was selected on the priv. list):
#ManagedBean(name="adminBean")
#ViewScoped
public class AdminBean {
private String user_id ="";
private String login ="";
private String password ="";
private ArrayList<String> privilegeList = new ArrayList<String>();
private User model = new User();
private TreeMap<String, User> usersValuesBackendMap = new TreeMap<String, User>();
private TreeMap<String, String> privilegesValues = new TreeMap<String, String>();
private TreeMap<String, String> myModelUsersValues = new TreeMap<String, String>();
...
#javax.annotation.PostConstruct
public void init()
{
usersValuesBackendMap = queryDAO.getAllUsers();
for (Map.Entry<String, User> usr : usersValuesBackendMap.entrySet()) {
myModelUsersValues.put(usr.getValue().getLogin(), usr.getKey() );
}
privilegesValues = queryDAO.getFullPrivilegeList();
user_id = "";
}
public void userSelected(ValueChangeEvent event){
String newValue = event.getNewValue().toString();
User user = usersValuesBackendMap.get(newValue);
login = user.getLogin();
password = user.getPassword();
privilegesValues.clear();
for (String privilege: user.getPrivilegeValues() ){
privilegesValues.put(privilege, privilege);
}
}
public String clear(){
user_id ="";
login ="";
password ="";
privilegesValues = queryDAO.getFullPrivilegeList();
return "";
}
Interestingly I added immediate="true" to the clearing method and then sth. opposite happens - the list is OK but the inputTexts are filled.
Some facts:
The valueChangeListener runs when !oldValue.equals(newValue).
The valueChangeListener is not a client side event listener. It's entirely server side.
The valueChangeListener is completely independent on whether it's an ajax or normal request. It's invoked on all types of requests, including the normal form submit.
You need <a4j:ajax listener> instead.
<h:selectOneMenu id="selectUser" value="#{adminBean.user_id}">
<f:selectItems value="#{adminBean.myModelUsersValues}" />
<a4j:ajax event="valueChange" listener="#{adminBean.userSelected}" render="login password privilege_list" execute="#this"/>
</h:selectOneMenu >
with
public void userSelected(AjaxBehaviorEvent event){
User user = usersValuesBackendMap.get(user_id);
// ...
}

Struts2 with Nested Iterator dynamic column and rows

I want to display grid in Struts2 which include dynamic rows and columns, it also provide that data should be save in database.
so i have created one list for columns and other map for that values in one bean.
I have included code also.
My bean looks like
public class Annexure{
private List<String> columnsList = new ArrayList<String>(1);
private Map<String,List<String>> columnsValues = new HashMap<String,List<String>>(1);
... setter/getter methods
}
Action class
package com.eks.ias.web.annexure.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.eks.ias.web.annexure.vo.Annexure;
import com.opensymphony.xwork2.ActionSupport;
public class AnnexureAction extends ActionSupport {
private static final long serialVersionUID = -8819437646232339486L;
private Annexure annexure = new Annexure();
public String execute()throws Exception {
List<String> columnsList = new ArrayList<String>();
columnsList.add("STNNo");
columnsList.add("EAN");
columnsList.add("ArticleCode");
annexure.setColumnsList(columnsList);
annexure.setTotalColumns(3);
annexure.setName("Stock Pending for Inward in SAP");
annexure.setDescription("Details of all merchandise physically received");
annexure.setSiteName("XXX");
Map<String,List<String>> columnsValues = new HashMap<String,List<String>>();
columnsValues.put("0", columnsList);
columnsValues.put("1", columnsList);
annexure.setColumnsValues(columnsValues);
return SUCCESS;
}
public void setAnnexure(Annexure annexure) {
this.annexure = annexure;
}
public Annexure getAnnexure() {
return annexure;
}
}
JSP page
<s:iterator value="annexure.columnsValues" status="rows">
<tr>
<s:iterator value="annexure.columnsList" status="columns">
<td><s:textfield name="annexure.columnsValues[%{#rows.index}][%{#columns.index}]" theme="simple"/></td>
</s:iterator>
</tr>
</s:iterator>
Html code generated looks like
<tr>
<td>
<input type="text" name="annexure.columnsValues[0][0]" value="STNNo"
id="annexure_annexure_columnsValues_0__0_"/>
</td>
<td>
<input type="text" name="annexure.columnsValues[0][1]" value="EAN"
id="annexure_annexure_columnsValues_0__1_"/>
</td>
<td>
<input type="text" name="annexure.columnsValues[0][2]" value="ArticleCode"
id="annexure_annexure_columnsValues_0__2_"/>
</td>
</tr>
when i submit data then i am not able to get those data in action
I am not able to understand the problem if data populate then data should get in action also.
Sorry I don't have enough rep to post a comment, and I'm not sure whether I understand your problem fully. However, there is one thing that I noted with the row index.
shouldn't this
<s:textfield name="annexure.columnsValues[%{#rows.index}][%{#columns.index}]" theme="simple"/>
rather be
<s:textfield name="annexure.columnsValues['%{#rows.index}'][%{#columns.index}]" theme="simple"/>
Note the single quotes. This should set the string key correctly in the map.
Code will be something like this
<s:iterator value="lstBean" id="lstBean" status="outerStat">
<s:textfield value="%{name}" name="lstBean[%{#outerStat.index}].name"/>
<s:textfield value="%{amt}" name="lstBean[%{#outerStat.index}].amt"/>
<s:textfield value="%{id}" name="lstBean[%{#outerStat.index}].id"/>
<s:iterator value="%{lstString}" status="myStat">
<s:textfield name="lstBean[%{#outerStat.index}].lstString[%{#myStat.index}]"/>
</s:iterator>
</s:iterator>
Here's the complete example on Nested Iterator in Struts2

Submitting a JSF2 form does not reload the collection on the same page

JSF 2.0 (mojarra) application. I have a very trivial form for adding an item
<h:form>
#{msg['add custom title']}:<br />
<table>
<tr>
<td>#{msg['heaading']}:</td>
<td><h:inputText value="#{titlesBean.title.heading}"></h:inputText></td>
</tr>
<tr>
...
</tr>
</table>
<h:commandButton action="#{titlesBean.addTitle}" value="#{msg['g.save']}" />
</h:form>
And then on the same page I have a list of all the items already added:
<h:dataTable id="manualTitlesForm" value="#{titlesBean.manualTitles}" var="title" border="1" cellspacing="0">
<h:column>
<f:facet name="header">#{msg['heaading']}</f:facet>
#{title.heading}
</h:column>
...
<h:column>
<f:facet name="header">#{msg['actions']}</f:facet>
<h:form>
<h:commandButton action="#{titlesBean.editManualTitle(title)}" value="#{msg['g.edit']}" />
<h:commandButton action="#{titlesBean.deleteManualTitle(title.id)}" value="#{msg['g.delete']}" />
</h:form>
</h:column>
</h:dataTable>
The code in the bean code is super simple:
#Controller
#Scope(Scopes.REQUEST)
public class TitlesBean {
private List<JTitle> manualTitles;
#PostConstruct
private void init() {
this.manualTitles = titlesManager.getManualTitles();
}
public String addTitle() {
title.setCreated(new Date());
title.setManual(true);
try {
titlesManager.addTitle(title);
title = new JTitle();// this is added, delete from the variable. only if no exception though !!!
UserMessagesBean.addMessage("saved");
} catch (Exception e) {
UserMessagesBean.setValidationException(e.getMessage());//different exception added
}
return null;
}
public List<JTitle> getManualTitles() {
return manualTitles;
}
}
Now the problem is that getManualTitles() is called as many times as the number of titles I have, which causes for example 12 calls to the DB, instead of one. Why is this happening is beyond my understanding. I can fix this with caching the manual titles in the bean. This is not my main problem.
The problem is that addTitle() is called AFTER getManualTitles(). In fact getManualTitles() is called for example 10 times, then addTitle(), then two more times the getManualTitles() method. This makes me think that this is some kind of parallel execution which causes my page to show only the 12 old records instead of 13. I have to reload the page, then 13 is shown.
UPDATED: now caches the list. Problem still not solved.
WHY? How can I fix this?
This is a quick fix, but not a real solution. Redirect the result of addTitle():
Add the following to addTitle():
...
FacesContext.getCurrentInstance().getExternalContext()
.redirect("manualTitles.jsf");
return null;
}

Resources