PDA

View Full Version : [JAVA]: portlet


mercury841
10-07-2008, 15:46
Ciao,
ho creato una portlet con una preferenza dichiarata all'interno del file portlet.xml e l'ho deployato senza problemi all'interno di JBoss Portlet Container. Il file xml per questa portlet č il seguente:



<portlet>
<description>PreferencesPortlet</description>
<portlet-name>PreferencesPortlet</portlet-name>
<display-name>PreferencesPortlet</display-name>
<portlet-class>it.portlet.PreferencesPortlet</portlet-class>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>VIEW</portlet-mode>
<portlet-mode>EDIT</portlet-mode>
</supports>
<portlet-preferences>
<preference>
<name>userName</name>
<value>Mark</value>
<read-only>false</read-only>
</preference>
<preferences-validator>it.validator.UserNameValidator</preferences-validator>
</portlet-preferences>
<portlet-info>
<title>Portlet Preferences</title>
<short-title>PreferencesPortlet</short-title>
</portlet-info>
</portlet>



Il mio problema si presenta quando cerco di modificare il valore della preferenza a runtime. In poche parole provo a modificare il valore della preferenza attraverso l'utilizzo del metodo "store" perņ quando cerco di recuperare la prefernza ottengo sempre il vecchio valore.
Per quale motivo non riesco a impostare il valore della preferenza a runtime?
Di seguito ci sono gli altri sorgenti:

public class PreferencesPortlet extends GenericPortlet {



public void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {


System.out.println("*************************************************** doView");
response.setContentType("text/html");

String userName =(String)request.getPreferences().getValue("userName",null);
if (userName != null)
response.getWriter().println("Hello " + userName);
else
response.getWriter().println("Vai nell'edit mode per modificare l'username");




}


public void processAction( ActionRequest actionRequest, ActionResponse actionResponse)
throws PortletException, IOException {

System.out.println("*************************************************** processAction");
String userName = actionRequest.getParameter ("userName");

System.out.println("*************************************************** username="+userName);

PortletPreferences pref = actionRequest.getPreferences();
pref.setValue("userName",userName);

try{
pref.store();
System.out.println("*************************************************** store");
}catch (Exception e) {
e.printStackTrace();
}

actionResponse.setPortletMode(PortletMode.VIEW);

}

@Override
protected void doEdit(RenderRequest request, RenderResponse response)
throws PortletException, IOException {

System.out.println("*************************************************** doEdit");
response.setContentType("text/html");
PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher("/WEB-INF/jsp/edit_userName.jsp");
dispatcher.include(request, response);


}


}

edit_userName.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>


<portlet:defineObjects/>
<h3>Edit User Name</h3>


<form method="post" action="<portlet:actionURL/>">
Username: <input type="text" name="userName" >

<input type="submit"> <input type="reset">
</form>



</body>
</html>



ciao e grazie in anticipo

mercury841
11-07-2008, 14:15
up