Commits

Micha Kops committed 48e9da8

Example for Portlet modes mapping in the JSF bridge added

Comments (0)

Files changed (5)

src/main/java/com/hascode/tutorial/jsf/UserBean.java

 package com.hascode.tutorial.jsf;
 
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
 import javax.faces.bean.ManagedBean;
-import javax.faces.bean.ViewScoped;
+import javax.faces.bean.SessionScoped;
+import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
+import javax.portlet.ActionResponse;
+import javax.portlet.PortletMode;
+import javax.portlet.PortletModeException;
 
 @ManagedBean
-@ViewScoped
+@SessionScoped
 public class UserBean {
-	public String[] getUserNames() {
-		return new String[]{"adam", "barry", "douglas", "ethan"};
+	private String				sortType	= "normal";
+
+	private final List<String>	names		= new ArrayList<String>();
+	{
+		names.add("adam");
+		names.add("barry");
+		names.add("douglas");
+		names.add("ethan");
+	}
+
+	public List<String> getUserNames() {
+		if ("reverse".equals(sortType)) {
+			Collections.reverse(names);
+			return names;
+		}
+
+		Collections.sort(names);
+		return names;
+	}
+
+	public void save() throws PortletModeException {
+		// Switch the portlet mode back to VIEW.
+		FacesContext facesContext = FacesContext.getCurrentInstance();
+		ExternalContext externalContext = facesContext.getExternalContext();
+		ActionResponse actionResponse = (ActionResponse) externalContext.getResponse();
+		actionResponse.setPortletMode(PortletMode.VIEW);
+	}
+
+	public String getSortType() {
+		return sortType;
+	}
+
+	public void setSortType(String sortType) {
+		this.sortType = sortType;
 	}
 }

src/main/webapp/WEB-INF/faces-config.xml

 			<var>i18n</var>
 		</resource-bundle>
 	</application>
-	<!-- <lifecycle>
-		<phase-listener>org.portletfaces.example.util.DebugPhaseListener</phase-listener>
-	</lifecycle> -->
 	<navigation-rule>
-		<from-view-id>/xhtml/viewMode.xhtml</from-view-id>
+		<from-view-id>/xhtml/editMode.xhtml</from-view-id>
 		<navigation-case>
 			<from-outcome>success</from-outcome>
 			<to-view-id>/xhtml/viewMode.xhtml</to-view-id>

src/main/webapp/WEB-INF/portlet.xml

 		</init-param>
 		<supports>
 			<mime-type>text/html</mime-type>
-			<portlet-mode>view</portlet-mode>
-			<portlet-mode>edit</portlet-mode>
-			<portlet-mode>help</portlet-mode>
+			<portlet-mode>VIEW</portlet-mode>
+			<portlet-mode>EDIT</portlet-mode>
+			<portlet-mode>HELP</portlet-mode>
 		</supports>
 		<portlet-info>
-			<title>JSF2-Portlet-Bridge</title>
-			<short-title>JSF2-Portlet-Bridge</short-title>
-			<keywords>JSF2-Portlet-Bridge</keywords>
+			<title>hasCode JSF2-Portlet-Bridge</title>
+			<short-title>hasCode JSF2-Portlet-Bridge</short-title>
+			<keywords>hasCode JSF2-Portlet-Bridge</keywords>
 		</portlet-info>
 	</portlet>
 

src/main/webapp/xhtml/editMode.xhtml

 	<h:head />
 	<h:body>
 		<h:outputText value="EDIT" />
+		<h:form>
+			<h:messages globalOnly="true" />
+			<h:selectOneMenu id="selectSortType" value="#{userBean.sortType}">
+				<f:selectItem id="sort-normal" itemLabel="Normal Sorted" itemValue="normal" />
+				<f:selectItem id="sort-reverse" itemLabel="Reverse Sorted" itemValue="reverse" />
+			</h:selectOneMenu>
+			<hr />
+			<h:commandButton actionListener="#{userBean.save}" value="Submit" />
+		</h:form>
 	</h:body>
 </f:view>

src/main/webapp/xhtml/viewMode.xhtml

 <?xml version="1.0" encoding="UTF-8"?>
 <f:view xmlns:f="http://java.sun.com/jsf/core"
 	xmlns:h="http://java.sun.com/jsf/html"
-	xmlns:ui="http://java.sun.com/jsf/facelets">
+	xmlns:ui="http://java.sun.com/jsf/facelets"
+	xmlns:portlet="http://java.sun.com/portlet_2_0">
 	<h:head />
 	<h:body>
 		<h:outputText value="VIEW" />