Setting up a JSF project without maven - java

I used to build my projects with maven. Now I want to do it 'manually'. But I struggle a little bit with directory order and other stuff. I first just created a new dynamic web project in eclipse and added JSF libraries. Now I tried to deploy a hello world page onto a tomcat 7. But jsf-tags are not getting rendered.
Here is my directory structure:
Anybody has an idea where the mistake is? Am I missing a library or is my structure wrong?
cheers
It finally works! thank's to balusc

But jsf-tags are not getting rendered.
This means that the FacesServlet isn't mapped in web.xml or you didn't make the URL in browser address bar to match the url-pattern of the FacesServlet. The FacesServlet is the one responsible for parsing JSF tags and doing all the JSF works.
Assuming that the url-pattern of the FacesServlet as definied in web.xml is *.jsf, then you need to open the start.xhtml by http://localhost:8080/fitnessverwaltung/start.jsf instead of http://localhost:8080/fitnessverwaltung/start.xhtml.
You can also change the url-pattern to *.xhtml, then you don't need to worry about this.
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

Related

Root "/" mades server load index.html before pass through the servlet

I have a Servlet that is mapped to the root directory "/":
<servlet>
<servlet-name>Main</servlet-name>
<servlet-class>com.motorola.triage.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Main</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
In this servet there is a couple of small things that are done there, like authentication and retrieve of Google Plus information. After that, I'm doing a forward to a JSP file called "index.jsp"
req.getRequestDispatcher("index.jsp").forward(req,resp);
When I'm accessing "localhost:8080/" the static file "index.jsp" is loaded without passing through the servlet. For architecture reasons I can not change the name of index.jsp. I would like to ask if is there any way to change this behavior of the server and make it look to the servlet before look the index.jsp file.
This is occurring specifically because you used the name index.jsp.
This has been covered elsewhere, such as here and here and here.

Running .jspx on apache tomcat

We run an application written with "jspx" (Java Server Page with XML I guess), it runs on web-logic but the web-logic is down currently.
I wish to know if I could copy the files and put it under Apache tomcat.
I have actually tried that but I got some errors which makes me feel Apache tomcat is strictly for "jsp" and not "jspx".
If my assuption is right then what else can I use to compile a ".jspx" program aside from weblogic?
As far as I know, jspx are simply jsp files with well-formed XML instead of "just any html".
Try editing your tomcat/conf/web.xml and add another mapping for the jsp-servlet:
<!-- Existing mapping -->
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<!-- New mapping -->
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>
But... for Tomcat 8 this is already in place. Can you share the error messages you got?

SpringMVC Map Servlet to Root without Removing Content Servlet

Currently, we have "root" (/) mapped to a static index.html page, but we want to upgrade to a jsp to have dynamic content. Trying to figure out how to do this. We have content that is mapped to the default content server (e.g. /css), so we don't want to change too much.
We tried:
Changing the .html to .jsp. This resulted in a blank page.
Changing the .html to .jsp and then moving the file into the WEB-INF directory. This resulted in a 404.
Trying to subclass the DefaultServlet class that content servlet is currently mapped to. This through a 500, with a class assertion error (it checked to see if it was the same class).
Adding another servlet to that url, but it overwrote the current one.
I've searched StackOverflow, but still haven't found an answer that works.
Thanks!
If I understand your question correctly, this is trivial using Spring MVC:
<mvc:default-servlet-handler/>
And in web.xml:
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<async-supported>true</async-supported>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/META-INF/spring/your-applicationContext-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Is this what you have tried already?
Just set up a controller method mapped to / that returns a view name, which is your jsp file. And make sure your view resolver is set up correctly. Any of the spring mac tutorial hellos world programs out there will show how.

Servlet Mapping for Weblogic 8, How to?

I have a WAR file with a web application that has been deployed to a weblogic stream.
The JSP part works fine, but it can't find the servlets. Possible due to the lack of mapping in my web.xml file.
I was working fine on Tomcat 6, but can't seem to find using weblogic.
I used annotation #WebServlet("/actionOne") but this doens't seem to work.
I am a little confused about how to map these correctly via the web.xml file.
the servlets are .java files and located at WEB-INF/classes/com/foo/bar/
So far I have added the following the web.xml file but the servlet-mapping section has me confused.
<servlet>
<servlet-name>actionOne</servlet-name>
<servlet-class>com.foo.bar.actionOne</servlet-class>
</servlet>
<servlet>
<servlet-name>actionTwo</servlet-name>
<servlet-class>com.foo.bar</servlet-class>
</servlet>
Hopefully the above is correct, the next section I'm not sure how to use and would appreciate some help.
<servlet-mapping>
<servlet-name>actionOne</servlet-name>
<url-pattern>/actionOne</url-pattern>
</servlet-mapping>
The servlets are being called from the jsp via a Form action="actionOne"
My mapping was correct, it seems the issue was related to a different version of servlet.api in the weblogic modules folder. 2.5 instead of 3.0. This resolved the issue.

Can anyone explain servlet mapping?

I'm trying to write a web application using SpringMVC. Normally I'd just map some made-up file extension to Spring's front controller and live happily, but this time I'm going for REST-like URLs, with no file-name extensions.
Mapping everything under my context path to the front controller (let's call it "app") means I should take care of static files also, something I'd rather not do (why reinvent yet another weel?), so some combination with tomcat's default servlet (let's call it "tomcat") appears to be the way to go.
I got the thing to work doing something like
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>tomcat</servlet-name>
<url-pattern>*.ext</url-pattern>
</servlet-mapping>
and repeating the latter for each one of the file extensions of my static content. I'm just wondering why the following setups, which to me are equivalent to the one above, don't work.
<!-- failed attempt #1 -->
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>tomcat</servlet-name>
<url-pattern>*.ext</url-pattern>
</servlet-mapping>
<!-- failed attempt #2 -->
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>tomcat</servlet-name>
<url-pattern>/some-static-content-folder/*</url-pattern>
</servlet-mapping>
Can anyone shed some light?
I think I may know what is going on.
In your working web.xml you have set your servlet to be the default servlet (/ by itself is the default servlet called if there are no other matches), it will answer any request that doesn't match another mapping.
In Failed 1 your /* mapping does appear to be a valid path mapping. With the /* mapping in web.xml it answers all requests except other path mappings. According to the specification extension mappings are implicit mappings that are overwritten by explicit mappings. That's why the extension mapping failed. Everything was explicitly mapped to app.
In Failed 2, App is responsible for everything, except content that matches the static content mapping. To show what is happening in the quick test I set up. Here is an example. /some-static-content-folder/ contains test.png
Trying to access test.png I tried:
/some-static-content-folder/test.png
and the file was not found. However trying
/some-static-content-folder/some-static-content-folder/test.png
it comes up. So it seems that the Tomcat default servlet (6.0.16 at least) drops the servlet mapping and will try to find the file by using the remaining path. According to this post Servlet for serving static content Jetty gives the behavior you and I were expecting.
Is there some reason you can't do something like map a root directory for your rest calls. Something like app mapped to /rest_root/* than you are responsible for anything that goes on in the rest_root folder, but anywhere else should be handled by Tomcat, unless you make another explicit mapping. I suggest setting your rest servlet to a path mapping, because it declares the intent better. Using / or /* don't seem appropriate, since you have to map out the exceptions. Using SO as an example, my rest mappings would be something like
/users/* for the user servlet
/posts/* for the posts servlet
Mapping order
Explicit (Path mappings)
Implicit (Extension mappings)
Default (/)
Please correct anything that I got wrong.
For reference, the "failed attempt #2" is perfectly correct in version of Tomcat >= to 6.0.29.
It was the result of a Tomcat bug that get fixed in version 6.0.29:
https://issues.apache.org/bugzilla/show_bug.cgi?id=50026
<!-- Correct for Tomcat >= 6.0.29 or other Servlet containers -->
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/some-static-content-folder/*</url-pattern>
</servlet-mapping>
I've never tried to map a servlet like this, but I would argue that /* does technically both start with / and end with /*, even though the same character is used for both matches.

Categories