How do I expose bean in JSP? - java

How can I expose a Java bean to a JSP page by using struts? I know how to configure a StrutsAction to include a form-bean, but I wonder if there are other ways to interact with Java code from a JSP page? I ask this question because I don't understand fully a likely answer to a problem that I have asked here:
Clean way for conditionally rendering HTML in view?
EDIT:
I understand that a JavaBean is defined as a class that contains mainly getters and setters for its properties.
My problem was that I did not see how I can access parameters from Java classes in my JSP. Currently, I use a DynaForm to communicate parameters to the view. E.g. in the ActionClass I set the parameter, and in the JSP I can access it with
bean:write name="MyFormBean" property="myParameter"
My question was basically if there are other classes than a DynaForm class that can easily be accessed from inside the JSP with tags, and if so, if someone could provide an example.

In your action class:
MyBean myBean = new MyBean();
myBean.setSomeProperty("someValue");
request.setAttribute("myBean", myBean);
In your JSP:
<bean:write name="myBean" property="someProperty" scope="request"/>
You can do the same with session as well. Note that you don't have to explicitly specify the scope in <bean:write> tag - if you don't, Struts will look in all scopes from page to application.
More information on scopes is available in Java EE tutorial.

Related

What does <ice:outputText value="#{exam.testName}"/> mean?

I have a xhtml file with <ice:outputText value="#{exam.testName}"/>
The part of the webpage associated with that statement just shows the Test Name.
I have the whole java source code and I am trying to find the database query that brings back the test names. I don't know what exam.testName means and what it refers to in the code for it to get the test name for that page.
Thanks!
It is using a JSF (Java Server Faces) component; probably from the ICEFaces component library (you should search your xhtml namespaces to be sure, though)
It shows some text obtained from calling getTestName() in a bean identified as exam (either the bean class is Exam or it has a #Named annotation specifying such a name for the bean, or it is defined in the faces-config.xml file.

How to get to application context path without using scriplets in JSP files?

So I have a code like this in my jsp file:
<a href="<%= getServletConfig().getServletContext().getContextPath() %>/registerMe.jsp"
class="btn">Not a member? Register..</a>
And I know that using scriplets is bad practice in JSP files. How can I avoid such a situation then?
Use an EL expression:
<a href="${pageContext.servletContext.contextPath}/registerMe.jsp"
class="btn">Not a member? Register..</a>
You can use request.getContextPath() in your action class, and you can pass that to JSP as a string using request, or you can use bean to get that in JSP.
Application scoped objects are stored as attributes of the ServletContext. If the "function call" has access to the ServletContext, then it can just grab them as follows:
Bean bean = (Bean) servletContext.getAttribute("beanname");
I of course expect that the "function" is running in the servlet context. I.e. it is (in)directly executed by a servlet the usual way.
You can also try this link. It have example with full explanation.

Which variables can be accessed with the ${...} syntax in a Struts tag in a JSP page?

I'm getting a little bit frustrated since I can't find out which variables I can access with the ${...} syntax in a Struts tag, placed in a JSP page.
As an example I've got the following code:
<c:set target="${status.menue}" property="activeMenuePath" value="whatever" />
Where does the object "status.menue" have to be defined in order to can be accessed with a dollar sign and braces. Is it defined in another struts tile or in the form?
It should be placed in any of the page, request, session or application scopes using respectively JspContext#setAttribute(), ServletRequest#setAttribute(), HttpSession#setAttribute() or ServletContext#setAttribute(). You normally do that either directly or indirectly inside a Servlet. MVC frameworks do that indirectly, usually configureable by giving the model object a "request", "session" or "application" scope.
The Expression Language (EL) will access them using JspContext#findAttribute().
This all is by the way unrelated to Struts. It's just a legacy MVC framework which is built on top of the JSP/Servlet API. The <c:set> is not a Struts tag as well, it's a JSTL tag.

Clean way for conditionally rendering HTML in view?

Is there a cleaner way to do this in a JSP/Struts1 setup ?
... some HTML here ...
EDIT: In admin mode I would like to have access to additional parameters from a form element,
e.g. from the form element:
input type="text" value="Test user" name="Owner"
EDIT 2: Actually, my problem is very similar to the question that was asked in : Conditionally Render In JSP By User
But I don't really get the "pseudo-code" from the likely answer
Is SessionConfig exposed as a bean in your JSP (as part of request / session / Struts Form)?
If it's not, you can expose it. And if it's a static class containing global settings (which, by the looks of it, is a possibility), you can create a small wrapper and put it in the servlet context which you'd then be able to access from Struts tags as scope="application".
Once that's done you can check your condition via Struts tags:
<logic:equal name="sessionConfig" property="adminMode" value="true">
... your HTML here
</logic:equal>
Or, if you're using EL / JSTL, same can be done via <core:if>.
Without more information, it's hard to answer this, but I'd think instead of separate views: one for admin mode, one for normal mode. Extracting the parts of your pages into tiles will help you do this without a lot of pain; see: http://tiles.apache.org/

Java constants in JSP [duplicate]

This question already has answers here:
How to reference constants in EL?
(12 answers)
Closed 6 years ago.
I have a class that defines the names of various constants, e.g.
class Constants {
public static final String ATTR_CURRENT_USER = "current.user";
}
I would like to use these constants within a JSP without using Scriptlet code such as:
<%# page import="com.example.Constants" %>
<%= Constants.ATTR_CURRENT_USER %>
There appears to be a tag in the Apache unstandard taglib that provides this functionality. However, I cannot find any way to download this taglib. I'm beginning to wonder if it's been deprecated and the functionality has been moved to another (Apache) tag library?
Does anyone know where I can get this library, or if it's not available, if there's some other way I can access constants in a JSP without using scriptlet code?
Cheers,
Don
On application startup, you can add the Constants class to the servletContext and then access it in any jsp page
servletContext.setAttribute("Constants", com.example.Constants);
and then access it in a jsp page
<c:out value="${Constants.ATTR_CURRENT_USER}"/>
(you might have to create getters for each constant)
Turns out there's another tag library that provides the same functionality. It also works for Enum constants.
Looks like a duplicate of accessing constants in JSP (without scriptlet)
My answer was:
Static properties aren't accessible in EL. The workaround I use is to create a non-static variable which assigns itself to the static value.
public final static String MANAGER_ROLE = 'manager';
public String manager_role = MANAGER_ROLE;
I use lombok to generate the getter and setter so that's pretty well it. Your EL looks like this:
${bean.manager_role}
Full code at https://rogerkeays.com/access-java-static-methods-and-constants-from-el
What kind of functionality do you want to use?
That tag sould be able to access any public class field by class name and field name?
Scriptlets linking done at compile time but taglib class field access has to use such java API as reflection at runtime. Do You really need that?
I'll use jakarta-taglibs-unstandard-20060829.jar in my project but, you're true, it seems not available for download anymore.
I've got that in my pom.xml in order to get that library but I think It will work only because that library is now on my local repository (I cannot find it in official repositories) :
<dependency>
<groupId>jakarta</groupId>
<artifactId>jakarta-taglibs-unstandard</artifactId>
<version>20060829</version>
</dependency>
I do not know if there's another alternative.
I hope so because it was a good way to access constants in JSP.
Why do you want to print the value of the constant on the JSP? Surely you are defining them so that in the JSP you can extract objects from the session and request before you present them?
<%# page import="com.example.Constants" %>
<%# page import="com.example.model.User" %>
&lt%
User user = (User) session.getAttribute(Constants.ATTR_CURRENT_USER);
%>
<h1>Welcome <%=user.getFirstName()%></h1>

Categories