I'm trying to include JSP pages with jsp:param in a Portlet environment (using the Pluto portlet container).
for example,
<jsp:include page="test.jsp">
<jsp:param name="foo" value="bar"/>
</jsp:include>
and in test.jsp,
<c:out value="${foo}"/> or <%= request.getParameter("foo") %>
The output is always null and i've also tried using c tags, but got the same result.
<c:import url="test.jsp">
<c:param name="foo" value="bar"/>
</c:import>
I've searched through the net and many people have faced the same problem, except that there is no solution to it.
Is this a limitation or is there a different way to do it?
This works fine in a normal Servlet environment, but I see from a bit of googling that the portlet environment seems to break it. This is a shame, but indicative that the portlet spec is, to put it bluntly, broken.
If <jsp:param> won't work for you, the alternative is to use request attributes instead:
<c:set var="foo" value="bar" scope="request"/>
<jsp:include page="test.jsp"/>
And in test.jsp:
<c:out value="${requestScope.foo}"/>
or maybe just:
<c:out value="${foo}"/>
It's not as neat and contained as using params, but it should work for portlets.
I had the same problem. My solution was to work with Portlet's renderRequest object (which is accessible from included jsp files). In my portlet I set the attribute on the RenderRequest object then in my JSP (included via jsp:include). I use Portlet API to access the implicit renderRequest object. Here is a sample:
MyPortlet:
public void doView(RenderRequest request, RenderResponse response) {
request.setAttribute("myBean", new MyBean());
getPortletContext().getRequestDispatcher("myMainJSP.jsp").include(request, response);
}
myMainJSP.jsp:
<jsp:include page="header.jsp"/>
header.jsp:
<%# taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<% page import="sample.MyBean" %>
<portlet:defineObjects/>
<%
MyBean myBean = (MyBean)renderRequest.getAttribute("myBean");
%>
... html code...
Related
I can't seem to access data I want to pass to my .jsp files. I've simplified it to the bare-bones and it still doesn't work for me. This portlet is being deployed to a liferay portal.
Problem: I click on the link to call a renderURL for the next .jsp page. In the doView() method, I set an attribute with setAttribute. When I retrieve that attribute in the .jsp, it returns null.
Expected output: "Proper Value"
Actual output: "Default value"
NewPortlet.java (Controller)
public class NewPortlet extends MVCPortlet {
#Override
public void doView(RenderRequest request, RenderResponse response)
throws IOException, PortletException {
request.setAttribute("test", "Proper Value");
String path = ParamUtil
.getString(request, "path", "/html/new/view.jsp");
include(path, request, response);
}
}
view.jsp
<%# taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<portlet:renderURL var="testURL">
<portlet:param name="path" value="/html/new/edit.jsp"/>
</portlet:renderURL>
<p>Click me!</p>
edit.jsp
<%# taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%#page import="com.liferay.portal.kernel.util.ParamUtil"%>
<portlet:defineObjects />
<%
String test = ParamUtil.getString(renderRequest, "test", "default value");
%>
<p><%= test %></p>
I am not sure what exact usecase you have.
But here issue is, you are setting attribute and trying to fetch parameter, hence it returns null.
For solution to your issues as in code, use renderRequest.getAttribute("test") or use el, ${test}
The "view.jsp" is referring "edit.jsp" using portlet:renderURL and that can be an issue. The render requests may be executed sequentially or in parallel without any guaranteed order.
I am not sure why your workflow is this way (i.e. setting parameter in doView method), but try actionURL instead of renderURL.
I have a question, how to call a servlet from a jsp(chart.jsp) without using <jsp:include page="/servletURL" />because I tried before and I don't know if this is the right reason but it crashes when I use this code above.
I put in my doGet() method to retrieve information from DB and populate my dropdownlist (in chart.jsp) using JSTL+option and then redirect to my page (the same page), what I believe is that everytime the browser writes a new page using c:forEachtag it calls again my servlet and there is a never ending loop (Again, that's just my presumptions)
Here is my code to make it more clear:
my servlet:
ArrayList<Machine> foundMachines = MachineDB.getAllMachines();
request.getSession().setAttribute("foundMachineList", foundMachines);
RequestDispatcher rd = request.getRequestDispatcher("charts/chart.jsp");
rd.forward(request, response);
my jsp:
<jsp:include page="/searchServlet" />
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:forEach var="machine" items="${sessionScope.foundMachineList}">
<option value="${machine.machineId}">${Machine.machineName}
</option>
</c:forEach>
So my question is why my <jsp:include page="/servletURL" /> tag crashes my page and how to fix it. Any sugestion is welcome
Use
response.sendRedirect("//your servlet name");
Most probably is not related but change
${Machine.machineName} to ${machine.machineName}
I am beginner in java and trying to figure out how to send parameter from jsp to servlet using this
inside my jsp -
<%! String option;%>
<% option = request.getParameter("option"); %>
<jsp:useBean id="processBean" scope="session" class="helpers.ProcessBean" />
${processBean.processRequest(option)}
I want to send 'option' to processRequest but this way it is always empty string, so I am doing something wrong.Could not find anything online.
Any help is highly appreciated.
Variable declared/set on the page's scripting language is not automatically available from EL. You can manually make it available via:
<c:set var="option" value="<%= option %>" />
before using it in EL.
I'm writing a web app that uses a servlet to maintain an ArrayList of VideoData objects (these just contain basic information about movies like the title, type of movie, etc).
The servlet puts this List in the request's scope and forwards both the request and response to a jsp (only part of the servlet code is shown here):
public class VideoServlet extends HttpServlet {
private ArrayList<VideoData> library = new ArrayList<VideoData>();
public void doGet(HttpServletRequest request,
HttpServletResponse response) {
try {
// put ArrayList in Request's scope
request.setAttribute("the_table", library);
request.getRequestDispatcher("/listvideos.jsp").forward(request,
response);
...
The listvideos.jsp is shown below. I'm getting a Tomcat error stating that the uri for the JSTL cannot be resolved. I've used EL in other parts of my jsp code without having to have any special import line like this, and I'm not sure if JSTL is still the preferred way to solve this type of problem while still trying to adhere to MVC2 and keeping all the Java code in the Servlet. Can anyone point me in the right direction here? Ideally I'd like a pure EL solution, if that's possible.
<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'
'http://www.w3.org/TR/html4/loose.dtd'>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Cattle Drive Assignment Servlets-4: Videos</title>
</head>
<body>
<h1>Cattle Drive Assignment Servlets-4: Videos</h1>
<form method='post' action='/videos/VideoServlet'>
Add a video
<br>
<br>
<table border="1">
<tr>
<th>Title</th>
<th>Star</th>
<th>Type</th>
<th>VHS</th>
<th>DVD</th>
<th>Description</th>
</tr>
<c:forEach items="${the_table}" var="movie">
<tr>
<td>${movie.getTitle()}</td>
<td>${movie.getStar()}</td>
<td>${movie.getType()}</td>
<td>${movie.inVHS()}</td>
<td>${movie.inDVD()}</td>
<td>${movie.getDesc()}</td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
Your code looks basically correct. Looks like the error you're seeing indicates that the JSTL taglibs cannot be found in the classpath. Please make sure that jstl.jar and standard.jar are in your war's WEB-INF/lib folder.
I want to create own header.jsp file instead of the one included in the JBoss Portal 2.6 but have to support the locale set by the user.
The original header.jsp does not contain any i18n and I don't know how to do it, especially how to get the actual locale.
In header.jsp, use a scriptlet:
<%# taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%
locale = request.getLocale() // get the user's locale from the HttpServletRequest
%>
and then set the property bundle:
<fmt:setLocale value="<%= locale %>" />
<fmt:setBundle basename="header" />
And then you can create
header_en.properties
header_de.properties
for customized messages.
You can look to Thread.currenThread method to see the user's lang.
After that you have to a framework to implement i18n, ex : struts or jsf.