burntehsky burntehsky - 8 days ago 5
Java Question

Portlet overridden method does not thrown exception

I am working on an example portlet that is supposed to show me users logged in when logged into luminus portal as admin and students logged in only see themselves and keep running into errors in my code so I am not sure how to fix these issues

This is the portlet code I am getting

overridden method does not thrown exception
when I added the
PersonException


package com.test;

import javax.portlet.GenericPortlet;
import javax.portlet.ActionRequest;
import javax.portlet.RenderRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderResponse;
import javax.portlet.PortletException;
import java.io.IOException;
import javax.portlet.PortletRequestDispatcher;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sghe.luminis.person.entity.Person;
import com.sghe.luminis.person.exception.PersonException;
import com.sghe.luminis.person.services.PersonService;
import java.util.List;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
* WebApplication6 Portlet Class
*
*
*/
public class WebApplication6 extends GenericPortlet {


public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {

}



private static final Log cLog = LogFactory.getLog( WebApplication6.class );



public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException, PersonException {
PortletRequestDispatcher dispatcher =
getPortletContext().getRequestDispatcher("/WEB-INF/jsp/WebApplication6_view.jsp");
dispatcher.include(request, response);



response.setContentType("text/html");
PersonService personService = PersonServiceFactory.instance().getPersonService();
List<Person> persons = personService.fetchPagedPersons( "givenname", "", 10, "1" );
cLog.debug( "We have " + persons.size() + " people." );


}
}


This is the
/WEB-INF/jsp/WebApplication6_view.jsp
code I was given as an example to work with

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "=//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="javax.portlet.*"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<portlet:defineObjects />
<%PortletPreferences prefs = renderRequest.getPreferences();%>
<%@page import="java.util.List"%>
<%@page import="com.sghe.luminis.person.entity.Person" %>
<%
List<Person> persons = (List<Person>)renderRequest.getPortletSession().getAttribute("PERSON_LIST");
%>

<html>
<head>
<meta http=equiv="Content=Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>

</head>


<body>
<h1>Person List</h1>
<table class="summit2" border="1 padding="2px">
<theader>
<th>Display Name</th>
<th>Login Id</th>
<th>Person Id</th>
</theader>
<% for (Person person: persons) {%>
<tr>
<td><%= person.getDisplayName() %></td>
<td><%= person.getLoginId() %></td>
<td><%= person.getPersonId() %></td>
</tr>
<% } %>
</table>


</body>


</html>

Answer

While Paul is definitely correct about overriding and exceptions, I think the design of your solution could use some improvement.

Why would you want to throw that exception up to the user in the first place? I would suggest handling that exception in a more controlled manner in your code and then displaying a user-friendly error message. If you just let the exception throw all the way to the top it's going to look very ugly and make your application look unstable and unpolished.