Spring MVC - generic HTTP handlers - ajax

Have searched around and have not found a conclusive answer to this.
I have trying to route all http requests through my dispatcher servlet, and then onto a specific controller. Ultimately I want to be able to handle resource, AJAX and a.n.other request through the central point.
I currently have the url mapping /* in place to do this. My controllers use #RequestMapping("/[My resource].*") to capture my .htm requests. Unfortunately Spring appears to use RequestDispactcher.forward to resolve the .jsp from the InternalResourceViewResolver which is then hitting the front controller again and ultimately causing a 404 error.
My question is, am I able to setup a generic catch all that will handle any HTTP request other than the regular view request ?
The HTTP handler must be able to pass requests on to other servers and resolve internal and external resources e.g. images, css etc.
Regards,
Andy
Regards

A think a better idea is to change the servlet-mapping of DispatcherServlet to / instead of /*, this is because /* makes all request come to this servlet, instead like you have found for the jsp forwards also, inspite of the fact that there is a JSPServlet mapping for the jsps, the / mapping on the other hand will be defaulted to only if a specific mapping is not found for the requested path.

Keep the app servlet mapping to / in web.xml. Like shown below.
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
To resolve other resources add following tag in your dispatcher servlet xml.
Here resources is the folder containing js, css, images. It is stored under Webcontent folder in maven web application structure. Change it according to your project structure.
<resources mapping="/resources/**" location="/resources/" />
Try this.

Related

How to make an AJAX call in Magnolia Blossom?

I am new to Magnolia Blossom.
I have to perform an AJAX call in my application in Blossom.
We have a Controller per component. So I am unable to make an AJAX request.
Can anyone suggest how can I achieve this?
you may define a different Servlet (and web application context), you can define a servlet that handles all the requests that start for /rest/* and then below that on your web.xml you can define the blossom servlet.
All the rest is configuration, try to see how to create a webapp with 2 different contexts.
The controllers you're using for rendering content are not accessible to requests coming in to the servlet container. Without content they're pretty useless and won't generate meaningful output. You need a separate DispatcherServlet handling these AJAX requests.
There's two ways to achieve this. You can either add a new DispatcherServlet to web.xml or you can add a servlet to your module that is installed when your module is installed.
The latter is the better choice because you won't need to have two separate ApplicationContexts. The one created in your module on startup will be the parent of both DispatcherServlets so both can access beans within it. You also won't need to update web.xml which makes module install easier and upgrades of Magnolia easier.
In your module descriptor add this snippet:
<servlets>
<servlet>
<name>dispatcher</name>
<class>org.springframework.web.servlet.DispatcherServlet</class>
<mappings>
<mapping>/ajax/*</mapping>
</mappings>
<params>
<param>
<name>contextConfigLocation</name>
<value>classpath:/ajax-servlet.xml</value>
</param>
</params>
</servlet>
</servlets>
This, and other topics, is described in the Magnolia wiki https://wiki.magnolia-cms.com/display/WIKI/Adding+a+DispatcherServlet+to+a+Blossom+module
Depending on what you want to get out, you can also use REST module of Magnolia. To e.g. read a title of website you can just call http://localhost:8080/magnoliaAuthor/.rest/properties/v1/website/demo-project/siteTitle more details at documentation
And you can use REST module to also very easily add your own endpoints by just annotating source code.
HTH,
Jan

Fail to serve .jsp and .html file via default servlet handler using spring mvc <mvc:default-servlet-handler />

The title should explain the biggest part :)
I should be able for example to access http://www.someurl.com:8080/index.jsp but instead I get HTTP Status 404 - /index.jsp
Now why do I assume I should be able to serve static content (ie not be redirected to custom controller but to default servlet handler in stead.)?
Because I have added the following element to my mvc dispatcher servlet config:
<mvc:default-servlet-handler />
I have read that in some case the name of the default server cannot be guessed and I have looked it up in the file: ̣*~/tomcat7/conf/web.xml .*
The default servlet is specified by a name "default". So I tried adding:
<mvc:default-servlet-handler default-servlet-name="default"/>
But to no avail...
I have one spring dispachter servlet mapped to '/'.
I have two controllers mapped to, one controller is mapped to '/' and one is mapped to '/todo'
The controllers work fine.
I thought the controller mapped to '/' could be the culprit so I removed that controller but to no avail.
Anybody an idea? And is it possible to have a controller mapped to '/' and still serve a page like /index.jsp??
Arf, I had put my static resources under the webapp/WEB-INF folder instead of the webapp folder. Now it seems te be working fine ... :)

I do not want to map all my xhtml pages by a controller

But I want to use Spring security.
I think i have to use DispatcherServlet and its configuration in web.xml
I am developing an application that is nor jsp nor jsf project, i am going to make all connection based on javascript/ajax/jquery via server communication.
Thus i do not want to map my xhtml pages to a controller.
But i have a single controller with #RequestMapping(/auth/login) i only want it to run when i request /auth/login this is not the problem, it is working excellently.
But when i use
spring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:META-INF/spring-servlet.xml
1
spring
/heythere/*
and call http://localhost:8080/app/myhtml.xhtml it tells me i have no mapping for this uri.
I do not want mapping, nor controller to run, only want to see the page.
But DispatcherServlet needs to map it, how can i tell DispatcherServlet not to map ordinary xhtml pages?
Option 1:
Inside your spring web mvc application context XML you should put something like:
<mvc:view-controller path="/myhtml.xhtml"/>
The downside is you'll have to do this per page.
Option 2:
Use a Resource Handler:
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
<mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/static/**"/>
Your page would be visible like http://localhost:8080/app/static/myhtml.xhtml.
More info can be found in Spring's Doc.

Websphere not using welcome-file-list after mapping *.html to a servlet

I am using IBM WebSphere (WAS) 7.0.0.19 to host a java-based web-app, and I needed to map the extension *.html to a particular servlet so that I could do some server-side scrubbing of user-supplied HTML files. (The server reads the file, augments it with some extra information, and serves up the modified content transparently to the person viewing the page.)
Unfortunately, when I did this, welcome-files stopped working. Previously, if I typed in the URL for a directory, the server would look for index.html and serve up that. Now, I'm just getting a 403 forbidden rule ("Forbidden - by rule."). The access logs don't show anything more--they simply state that directory indexing is forbidden by rule for the server, which is correct. I don't want the webserver to build a table of contents for directories with no index.html, but when there is an index.html, I want it to serve up that file.
My first thought was that it was trying to serve the index.html through my servlet, the servlet was failing to find the file (because the url lacked "index.html"), and therefore it thought there was no index.html. However, I put in some debug code and am quite confident that the servlet code is never getting run when I go simply to the directory itself.
I don't really care whether index.html is served through the servlet or not--in the case of this particular file, the servlet would just spit back the original file anyway. I just want index.html to be served by something.
Here is the relevant section of my web.xml
<servlet-mapping>
<servlet-name>PageScrubber</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
For what it's worth, index.htm and index.jsp were not working before the addition of the servlet mapping. Only index.html worked before. However, now none of them work.
I have used the same web.xml with two Oracle products: WebLogic (WLS) and Oracle Application Server (OAS) with no issues.
I am quite confident that it is just the addition of this scrubber servlet that has caused the problem, because removing that directive caused directory indexing to start working again.
I did find some notes about welcome-file-list not working when using an extended document root, and I tried setting com.ibm.ws.webcontainer.EnablePartialURLtoExtendedDocumentRoot to be true, but that did not seem to change anything.
I'm pretty much out of ideas. Does anyone out there have any thoughts as to why it's not finding my index.html? Thanks in advance!
Caveat: I am working out of memory here.
The Welcome files used to be served by File Serving Servlet (or something that sounds similar to that).
This information would be in the WebSphere extensions file.
I would take a step back and remove your pageScrubber and get the file serving Servlet to serve the welcome files and see that things are working before getting back to using the PageScrubber.
These are my initial thoughts.
HTH

JSP Including A Private Servlet

I have a servlet that responds to a URL and then forwards to a JSP in a typical MVC pattern.
Many pages share the same page head so in the JSP there is an include to head.jsp
head.jsp is placed inside WEB-INF so that it cannot be accessed directly.
Now I find that I need to add some control to the head. Rather than forwarding to WEB-INF/head.jsp and putting scriptlets in I would like to forward to a servlet instead.
How can I forward from the JSP to a servlet without mapping that servlet to a URL as I do not want to give direct access to this servlet.
Or to put it another way is there a servlet equivalent of WEB-INF to hide it from direct access? So the servlet can only be called via an include?
Rather than forwarding to WEB-INF/head.jsp and putting scriptlets in I would like to forward to a servlet instead.
It's indeed possible to do this (using <jsp:include> or a small scriptlet that dispatches), but I'm not sure whether this is really the best approach. The Servlet would either write directly to the response or would put some data in the request scope that the JSP can pick up later.
Writing directly to the response is a bit debatable today and for the other approach you don't need a Servlet at all.
The idiomatic way is to use some helper bean that contains the logic. The original Servlet you mentioned can put this bean into request scope, or you can use the <jsp:usebean> tag. Reference the data the helper bean prepared via expression language or very simple scriptlets.
So the servlet can only be called via an include?
If you still want to go this route, there might be an option of securing the Servlet behind a role and then giving the head.jsp a run-as role in web.xml:
<servlet>
<servlet-name>headInclude</servlet-name>
<jsp-file>/WEB-INF/head.jsp</jsp-file>
<run-as>
<role-name>SYSTEM</role-name>
</run-as>
</servlet>
disclaimer: I have never tried this myself, just pointing in a possible direction.

Resources