include-prelude not being picked from web.xml using Spring 4 - java

Hi I'm putting together a fairly basic app using spring 4 MVC. I am using config java classes rather than xml config. I'm pretty new to this but all is slowly moving forward well. I have hit a problem though in that I wanted to include a header into all the jsp page views.
So I have included the include-prelude into my web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Web Application</display-name>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<include-prelude>/WEB-INF/views/include/header.jspf</include-prelude> </jsp-property-group>
</jsp-config>
</web-app>
This is working if I add a dummy jsp file into the webapp folder (I'm using maven and eclipse) and access it directly. However it is not working for the JSP's accessed via spring MVC. It is working in a similar application I inherited which has the spring bootstrap config in xml files rather than java classes. I won't have millions of pages so I guess I can use a jsp:include but...
Can anyone tell me how I should go about getting the header.jspf picked up ? Ideally I'd like to keep the config in java classes but perhaps I have to use the xml bootstrapping ?
Also as a supplementary question which is not really what I'm asking so please ignore if it's against all the rules (!) when I've googled this a bit I keep reading that JSP's are no longer the way to go for views. I'm writing a fairly basic intranet forms app (I've recently switched from microsoft technologies so apologies if you don't like that terminology!). Do you think I should be using somethign other than JSPs & jspf's
Thanks

In case you are wondering what is the answer to the OP'S question, the answer is to simply add this to your web.xml tag (if you are using Tomcat instead of GlassFish):
xmlns:my="http://jakarta.apache.org/tomcat/jsp2-example-taglib"
So your web.xml should look like this at the beginning:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xmlns:my="http://jakarta.apache.org/tomcat/jsp2-example-taglib">
I hope this could helped you ;)

You can use jsp.There is no problem with that the only thing you need to change is instead of giving url-mapping like .jsp use url mapping .abc, here you can use abc,xyz,ani,spring,do etc anything rather then jsp.So it will work fine .
If you use *.jsp in url mapping and use jsp as view it will give you 404 everytime.

Related

#WebServlet annotation web.xml welcome-file

I would like to set the welcome-file of my JSP/JavaBeans project. I have a servlet named 'Controller.java' with the following #WebServlet annotation:
#WebServlet(name="Controller", urlPatterns={"/login", "/show_dbs"})
and I hava a web.xml file with the following content:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<welcome-file-list>
<welcome-file>Controller</welcome-file>
</welcome-file-list>
</web-app>
Almost all things are going well, I can open http://localhost:8080/PROJECT/login and http://localhost:8080/PROJECT/show_dbs and I come to Controller.java. But when I open http://localhost:8080/PROJECT/ I get a 404 error.
I'm using Eclipse with a 'Dynamic Web Project', the Controller.java file is located under /src (default package) and the web.xml file is under /WebContent/WEB-INF.
I hope you have a tip for me.
Thank you for your help. Here comes my solution:
If you want to set your servlet as welcome file you have to do the following:
Define a standard html as welcome-file such as index.html in your web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Make sure this file (index.html) doesn't exist.
Define your urlPatterns in #WebServlet like this:
#WebServlet(name="Controller", urlPatterns={"/index.html", "/login", "/show_dbs"})
Now every request to http://.../PROJECT/ (root) will be redirected to http://.../PROJECT/index.html and this calls the servlet.
In the welcome file list you must specify the URIs. But you have specified the name of the servlet.
Quote from the Java™ Servlet Specification version 3.0 (emphasis mine):
10.10 Welcome Files
Web Application developers can define an
ordered list of partial URIs called welcome files in the Web
application deployment descriptor. The deployment descriptor syntax
for the list is described in the Web application deployment descriptor
schema.
The purpose of this mechanism is to allow the deployer to
specify an ordered list of partial URIs for the container to use for
appending to URIs when there is a request for a URI that corresponds
to a directory entry in the WAR not mapped to a Web component. This
kind of request is known as a valid partial request.
The use for this facility is made clear by the following common example: A welcome file
of 'index.html' can be defined so that a request to a URL like
host:port/webapp/directory/, where 'directory' is an entry in the WAR
that is not mapped to a servlet or JSP page, is returned to the client
as 'host:port/webapp/directory/index.html'.
If a Web container receives a valid partial request, the Web container must examine the
welcome file list defined in the deployment descriptor. The welcome
file list is an ordered list of partial URLs with no trailing or
leading /. The Web server must append each welcome file in the order
specified in the deployment descriptor to the partial request and
check whether a static resource in the WAR is mapped to that request
URI. If no match is found, the Web server MUST again append each
welcome file in the order specified in the deployment descriptor to
the partial request and check if a servlet is mapped to that request
URI. The Web container must send the request to the first resource in
the WAR that matches. The container may send the request to the
welcome resource with a forward, a redirect, or a container specific
mechanism that is indistinguishable from a direct request.
If no matching welcome file is found in the manner described, the container
may handle the request in a manner it finds suitable. For some configurations this may
mean returning a directory listing or for others returning a 404 response.
P.S.
Also see the examples in the specification in the chapter 10.10
This is what your web.xml should be. Create an index.jsp. Use the index.jsp as your welcome file. The controller class is your sevlet. So define a servlet in your web.xml as follows. This should cause all requests to be directed to the Controller class. And you should still be able to access localhost:8080/PROJECT/, in which case it will be directed to your welcome file.
If you don't want to create index.jsp, you can use your existing jsp file, may be your login.jsp file as your welcome file. In which case, just replace the index.jsp with login.jsp
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>com.company.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>*</url-pattern>
</servlet-mapping>
</web-app>
You can specify the url "/Controller" in urlPatterns in #WebServlet annotation. I think it will work.
The default welcome file is index.html, so just add the URL pattern.
#WebServlet(name="Controller", urlPatterns={"/index.html","/login", "/show_dbs"})

Deploy simple Jax-RS example in JBoss community AS 7

I'm trying to deploy a simple REST example in JBOSS AS 7 but seems that I'm doing something wrong and I'm now clueless. I'm using Eclipse plugin to deploy.
The class I wrote is the following
#Path("/resources")
#Consumes({MediaType.APPLICATION_JSON})
#Produces({MediaType.APPLICATION_JSON})
public class ReceivedImagePersister {
#POST
#Path("/image")
public Response save(String entry) {
return Response.ok().build();
}
}
Then I create a web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>API</display-name>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
</web-app>
And a jboss-web.xml
<jboss-web>
<context-root>api</context-root>
</jboss-web>
When I deploy the application and try to send some data to the RESTful servlet the response is always:
(Could not find resource for relative : /image of full path: http://localhost:8080/api/resources/image)
Do I have to make some changes in configuration?
Thanks a lot in advance.
I think your web.xml your url pattern should /* i.e.
<url-pattern>/resources/*</url-pattern>
should be
<url-pattern>/*</url-pattern>
What i am getting at is you need let javax.ws.rs.core.Application parse your url starting from http://localhost/api/ only then will it able to detect that there is a resource named "resources" which has method "image"
Right now it is trying to lookup a resource named "image" which is under the context path /api/resources/
The following url may work for the way its been setup now
http://localhost:8080/api/resources/resources/image
I don't have any experience with JBoss but you should probably use a Servlet 3.0 web.xml instead of a 2.4 if you want the best compatibility with the Java EE 6 deployment model.
The correct url for accessing what you have declared is:
http://localhost:8080/api/resources/resources/image
The first resources is the servlet mapping, and the second is the name of the rest controller.

Jetty, Maven plugin - how to configure default document?

We're using Jetty 6.x, and Maven-2.
Anybody knows how to configure the default document?
I mean - there's a default that serves /index.html or any of it's equivalents when accessing the application root (browse to /).
And there should be a settings to control that.
(there is in tomcat and in IIS for example...)
So 1 - how do I tell it to Jetty?
I know the Jetty guys are proud in letting almost everything to be configured - there has to be a way to do that, it's just a problem of a poor documentation, that's what my instinct tells me.
And 2 - how do I do that using Maven plugin ?
This is actually a "bonus" question, if you only tell me how to do it with Jetty - I hope'll find eventually how to do it with Maven too. But in case anybody knows - it will be a great help :)
Ok, I found it.
It has nothing to do with Jetty or Maven, although I'm sure that Jetty can provide it's own overrider configuration or defaults or something.
It was hard to find because they don't call it default document, but welcome-files.
It's a part of the JSP Servlet deffinitions, and is working with the file: /META-INF/web.xml
And here's what needs to be inside
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>customDefaultDocument.html</welcome-file>
</welcome-file-list>
</web-app>

GAE logout url giving error 404

My GAE java based application uses only one google user - the admin. For the admin web pages I generate the logout url using
UserServiceFactory.getUserService().createLogoutURL("/")
The generated url is always having a /zero at the end and clicking on it gives 'Error 404 NOT_FOUND'.
I The problem occurs on development server as well as the cloud. On dev server, this generated url is always looking like - http://localhost:8080/myapp/myurl/0 and when actually deployed on cloud it is similar http://myapp.appspot.com/myapp/myurl/0
I wonder why logout url generated is not working, is it something I am doing wrong or missing some configuration ? please help.
Check your web.xml. You have to add following section.
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
You can replace index.jsp with your choice.
Edit
I don't know what is wrong with your app. Here is a test app i have created.
http://rqtest123.appspot.com/
My web.xml look like
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
I think you shoul check your web.xml again.
Finally found it !!!
Earlier, through my spring controller I was passing the created logout url as
model.put("logout-url", UserServiceFactory.getUserService().createLogoutURL("/"));
And my JSP code looked like -
<a class="link" href="${logout-url}">Logout</a>
The variable name logout-url was the problem. Replaced it with logoutUrl and everything worked fine ! The - is not allowed in variable name.

EL in a JSP stopped evaluating

In a JSP page(index.jsp):
${requestContext.requestURL} is the URL
just shows the expression itself. It used to be evaluated to something like "http://.../somerset/"
I created the Maven project with maven-archetype-webapp archetype in Eclipse. The Jetty version is jetty-6.1.14.
My web.xml is simple:
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>SomersetServlet</servlet-name>
<display-name>SomersetServlet</display-name>
<description></description>
<servlet-class>com.foo.somerset.SomersetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SomersetServlet</servlet-name>
<url-pattern>/som.do</url-pattern>
</servlet-mapping>
</web-app>
See Javascript String.replace(/\$/,str) works weirdly in jsp file for some possible reasons.
Your web.xml should contain reference to web-app_2_4.xsd schema, like
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
This enables servlet 2.4 and jsp 2.0 processing, which includes EL.
Btw requestContext is not valid implicit object.
Incorrectly matched quotes can cause this behavior, where the expression just gets treated as a string. Your IDE would normally highlight this in a different color if this is the case.
Be sure you have directive, and other libraries you use included
<jsp:root .....
More info on definition here
http://java.sun.com/products/jsp/tags/12/syntaxref123.html

Categories