I have written a servlet using this tutorial from a Chinese developer works article
This servlet works fine for Helloworld printing but if a write a class and try to access that from the servlet doGet I get Error 500.
My servlet Class and ServletFactory is same as in the Above My Reference Link
Your own classes have to implement the IServletFactory interface, too. Also you have to define your class name (with whole package name) in the Web-Inf/META_INF/services/com.ibm.xsp.adapter.servletFactory file.
Just add a line like this:
com.myclasses.MyClass
Related
I have a module PortalHook that contains :
my class XCustomJspBag implements CustomJspBag
my jsp file terms_of_use (custom_jsps/html/portal/terms_of_use.jsp)
I need to use the JournalArticle class in this jsp file but when I add the following import :
<%# page import="com.liferay.journal.model.JournalArticle" %>
It always get me an error :
Une erreur s'est produite à la ligne: [245] dans le fichier Java
généré:
[C:\x\bundles\tomcat-9.0.17\work\Catalina\localhost\ROOT\org\apache\jsp\html\portal\terms_005fof_005fuse_jsp.java]
Only a type can be imported. com.liferay.journal.model.JournalArticle
resolves to a package
Right now i'm stuck here.
(I've imported com.liferay.journal.pai-4.5.2.jar in the pom.xml)
Looking at the CustomJspBag interface, it looks like it's only revealing the name of JSPs contained in a given module. I read this as them being resolved as relatively simple strings, and not processed within your custom module at all.
The problem is that you're injecting JSPs into Liferay's core, and you want the JSP to be dependent on a module. By definition that's something that shouldn't be done statically, but can be created dynamically, through explicit runtime means.
One option that comes to my mind is to try a Dynamic Include (DI) in your custom JSP. While the documentation talks about existing dynamic includes in JSPs, you can totally make up your own, and insert them into your custom JSPs. Then implement a proper DI with the new key that you came up with. As far as I remember, DIs will be resolved and executed within their module's context, so you'd have all of your dependencies there.
My expectation is that you can implement your DI in the same bundle that also holds your custom JSP.
Another alternative is to use any other kind of runtime dispatches, but I think that they're resulting in more work than DIs (provided they work the way I assume they do)
I wonder if anyone can help. We are in the process of converting a Spring Webflow 2 application from using a jsp based view layer, to a Thymeleaf based view.
For this most part this is OK, but now I'm struggling to get Thymeleaf to access an object that we've put in the servletContext.
So, we have an object that is put in the servletContext as part of a bean (implementing ServletContextAware and InitializingBean)
For the sake of simplicity, lets say it is a string:
public class ReferenceDataBuilder implements ServletContextAware, InitializingBean {
public void setServletContext(ServletContext p_context) {
p_context.setAttribute("referenceData", "test text" );
}
In our jsp based views, we can access the referenceData object like this:
<p><c:out value="${referenceData}"/></p>
By the magic of Spring EL, it knows the various scopes it has access to (servletContext, flowScope, flashScope etc), and (I'm guessing?) searches each scope until it finds a matching property. The result is that:
<p>test text</p>
is rendered within the view.
In our thymeleaf template, we are trying to do the same thing:
<p th:text="${referenceData}"/></p>
But this simply returns an empty string. The view renders an empty string:
<p></p>
(but I think the EL is actually being returned as a null)
I'm pretty sure that if the referenceData object were a property of a scope such as flowScope or flashScope this would work - but its not, its a property of servletContext.
Does anyone know if thymeleaf can access the servletContext via EL? Perhaps theres a different syntax I need to use?
Cheers
Nathan
You could access usual maps via the #ctx object, which is of type SpringWebContext.
For example #ctx.locale, #ctx.httpServletRequest.contextPath, #ctx.servletContext or even #ctx.applicationContext for Spring applicationContext.
You could use direct method invocation
<p th:text="${#ctx.servletContext.getAttribute('referenceData')}">Whatever</p>
or the applicationAttributes variables map
<p th:text="${#ctx.servletContext.applicationAttributes.referenceData}">Whatever</p>
or event simpler using Spring implicit object
<p th:text="${application.referenceData}">Whatever</p>
I've seen a reference to a DefaultErrorView in Myfaces CODI but as usual the
documentation leaves everything to the imagination. I've really found CODI to be a
great JSF addon, but it would benefit such a lot from some examples.
Probably naively I was hoping to be able to catch the dreaded ViewExpiredException
with this code:
#Page(basePath = "/defaultErrorPage.xhtml")
public final class DefaultErrorPage extends DefaultErrorView {
}
...but all that happens after session timeout is that container security takes me
to the login page when I try to issue a get request (clicking on a h:link). Does
anyone know what I can do with this DefaultErrorView, anyone got an example?
Thanks!
In the JavaDoc of DefaultErrorView you see:
...
The class which extends this class will also be used as error-view in case of security violations
(if there is no special error-view configured via
{#link org.apache.myfaces.extensions.cdi.core.api.security.Secured#errorView()})
And in the Wiki you see e.g.:
#Secured
...
In case of a violation CODI will use the DefaultErrorView as navigation target (if configured).
...
and
(Security) Error pages
The following example shows how to create a default error page. It's just allowed to provide one default error page per application.
Instead of implementing ViewConfig it's required to implement the DefaultErrorView interface.
...
as well as the manual usage:
...
this.viewNavigationHandler.navigateTo(DefaultErrorView.class);
...
The Wiki also links a nice example and there you find:
http://code.google.com/a/apache-extras.org/p/myfaces-codi-examples/source/browse/community/src/main/java/org/apache/extras/myfaces/codi/examples/community/view/config/Pages.java
-> everything is fine with the documentation ;-)
Let's say I've got a register page & a register confirm page. I enter user
details into the register page, navigate to the register confirm page where
I can return back to the register page if there are any mistakes.
I'm going to use view parameters to make the registration data available
from the register page to the confirm page, and vice versa.
Supposing there are 20 items of data to be moving from page to page, that's
a lot of view parameters and a lot of setPropertyActionListeners, especially
as all the data is going to end up nicely packaged in a User object.
So what I want to do is input the data on the register page into the
properties of a User record and send a reference to it to the register
confirm page. What gave me an idea was seeing the BalusC WeakHashMap
converter. This is a JSF converter which has a static weak hash map and
generates a uuid as the value for a map entry and the object reference as
the key. So by specifying this as a converter for f:viewParam you send
the uuid in the query string.
This works fine. The issue I have is that on the register page I have to
get an instance of a User class with new. Then I can do:
<h:inputText value="#{bean.user.firstname}"/>
(etc...), and pass the user instance as a view parameter. It works fine from
the register to the confirm page. The issue is that when I perform the
reverse, sending the user reference back to the register page from the
confirm page I absolutely cannot prevent the register page backing bean
from re-instantiating the user object, after the setter has been called
as a result of the view parameter.
So the converter does it's job and retrieves the User object from the
hash map, calls setUser() in the backing bean, and then I see the
constructor for the User class firing.
I've tried calling new User() from the bean constructor, in #PostConstruct,
in a preRenderView (also checking if an ajax request), but nothing I try
prevents the work of the view parameter from getting wiped out if new is
involved. I'm sure there's a simple solution but I just can't see it right
now.
I'd be grateful for any suggestions for how to solve this problem.
The issue I have is that on the register page I have to get an instance of a User class with new.
So what code is initially creating this new User instance then? If you do this in the preRenderView handler, then you can simply check for null, can't you?
If the view parameter and converter haven't done their job, user would still be null and you create a new instance. The bean constructor and #PostConstruct won't do you any good here, since they both run before the view parameter does its thing, but the preRenderView event is guaranteed to run after it.
#ManagedBean
public class Bean {
private User user;
public void onPreRenderView() {
if (user == null) {
user = new User();
}
}
}
(Something to additionally consider is that the conversation scope already does exactly what you're trying to do here. This is part of CDI not JSF, but if you're running in a Java EE 6 Web Profile compliant AS (JBoss AS 6 or 7, Glassfish V3, Resin 4, ...) you already have it. Otherwise it's just an extra jar.)
After several attempts over more than a year to find a solid long term solution
to this problem, at last! I've found one. The solution comes in the form of the
Apache Myfaces CDI extensions project, aka Myfaces CODI.
This provides additional scopes such as the #ViewAccessScoped which ensures that
if a bean is referenced by a page then it is available for that page. Also
provided is support for conversation groups. In the scenario where I want to
pass an object reference from a register page to a register confirm page, the
confirm page can just access the registerView bean directly on the next request.
Alternatively you can #Inject one bean into another and access it on the next
request, or use f:setPropertyActionListener from the source page.
Myfaces CODI works fine with Mojarra and also with ajaxified component libraries
such as primefaces. The concept is similar to what is provided by Jboss Seam,
though I've found the additional scope support to be better thought out and I've
tested this on glassfish 3.1.1 with no problems.
If you're using #ManagedBean and scope annotations from the javax.faces.bean
package in your code, codi intercepts these annotations and uses it's own
CDI based versions, so you can convert to CDI simply by adding codi as a
dependency to your project and not changing any code.
For me this is like moving from black and white TV to colour TV, I wish I'd
found this stuff sooner.
CODI documentation
I would like to log the exceptions that are thrown when serving JSF files in the same way other exceptions are logged in our web application.
We annotate classes with logged exceptions with #LoggedExceptions and a MehtodInterceptor is matched against those classes with Guice AOP (This should be very similar for other implementations of aopalliance...)
The main problem is, that the method interceptor does not work. How can i intercept method calls on JSF-backing code?
You must replace the default el-resolver (<el-resolver> in faces-context.xml) with a Guice el-resolver, so that the jsf beans become instantiated by Guide.
Search for "Guice el resolver", or take this one (I can't guarantee it works). Also check this thread.
Also, read the top results of this google search