Spring3 - WebFlow - JSF -- Can't get mapping of '/' to work properly - spring

Sorry if this is a Newbie question, but I am trying to teach myself Spring MVC/WebFlow with JSF/Primefaces, and I've run into a snag setting it up...
If in web.xml, I set the MVC dispatcher to a catch all '/', then register #RequestMapping(value = "/{catchall}", method = RequestMethod.GET), in my controller. The page is served, but the resources files all have the {catchall} name prepended to the start of the name e.g.
If I use //127.0.0.1:8080/testpage
<link type="text/css" rel="stylesheet" href="/testpage/javax.faces.resource/jquery/ui/jquery-ui.css?ln=primefaces&v=2.2" />
This results in every resource being NOT FOUND, and returning a 404 error?
If instead of a 'catch-all', I set the MVC dispatcher to '/a/*', the perform the same test, e.g.
//127.0.0.1:8080/a/testpage, it works fine with the resources being shown as:
<link type="text/css" rel="stylesheet" href="/a/javax.faces.resource/jquery/ui/jquery-ui.css?ln=primefaces&v=2.2" />
I am trying to setup a system where the page is served dynamically from the datastore, and want the page to be - www.whatever.com/{pagename} - without any prefixed structure, or postfixed identifier (e.g. .jsp, .jsf, .xhtml, etc.)
I can post configs if required, but am sure I'm just missing something stupid!!!!
Please help.

Last time I tried I found that the Sun Mojarra library assumes your servlet mapping is either a prefix mapping or a servlet mapping by extension (but not the default servlet mapping "/"). Your best bet to use URLs without a servlet prefix might be to use URL rewriting techniques such as Tuckey UrlRewriteFilter or in JSF PrettyFaces is quite popular.

Related

How to get absolute url as href/src value using Thymeleaf and Urlrewritefilter

I'm using Thymeleaf templating engine with Spring boot for developing my web application. For rewriting url, I'm using UrlRewriteFilter library.
My problem is that I couldn't able to rewrite url to a absolute url. For example, if script src value is configured in html like below
<script th:src="#{/js/jquery.js}"></script>
and rules defined in urlrewrite.xml as
<urlrewrite>
<outbound-rule>
<from>/js/jquery.js</from>
<to>https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-rc1/jquery.js</to>
</outbound-rule>
</urlrewrite>
The expected output is
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-rc1/jquery.js"></script>
But it is generating as
<script src="/myapphttps://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-rc1/jquery.js"></script>
where /myapp is context root. How to get rid of context root and get only absolute url which I configured.

Spring #RequestMapping("/favicon.ico") does not work when requested from index.html

I used to get mapping not found for favicon.ico so decided to deal with it.
Easiest thing for me was to add an action to a controller method,#RequestMapping("/favicon.ico").
I no longer get these complaints ( although I don't request favicon.ico myself in an html file, I guess teh browser does this automatically ).
When I visit http://localhost:8080/favicon.ico the action gets hit!
I add the following to my html file:
<link href="/favicon.ico" rel="icon" type="image/x-icon" />
But the action never gets hit.
I've also tried
<link href="http://localhost:8080/favicon.ico" rel="icon" type="image/x-icon" />
but action does not get hit.
I suspect this has to do with get/post request something, and when I request it from the browser manually, a get request is made. When from an html file something else, and Spring won't recognize this.
Please do not recommend me doing:
<mvc:resources mapping="/favicon.ico" location="/favicon.ico" />
as I like to do it from my controller as I have some logic there.
Could there be some cache involved?
EDIT:
I should also mention that i keep getting a tomcat favicon instead. Can't see any info on a favicon.ico ever being requested.
Is tomcat supplying it by default and ignoring to hit/forward it to my action?
I ran into the same issue.
When spring boot starts you can see this in the console:
[...] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
You can disable the spring mvc favicon handler from the application.properties:
spring.mvc.favicon.enabled=false
Source: Spring Boot: Overriding favicon

Can I move entire web.xml to spring code base configuration? (WebApplicationInitializer)

My application will use mainly use code based configuration. From web.xml to Springs WebApplicationInitializer class I already moved: servlet, filters and mapping. However in web.xml have much more elements (such as error-page or welcome page: https://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/web_xml.html). Which of those elements I can move to code and what are their equivalents?
Generally, yes, you should be able to get Java Config equivalents of all elements that you can set in the web.xml. In terms of converting individual elements, if you can give specifics, then we could find the JavaConfig equivalents.
Note that it's not always a 1-to-1 mapping. For example, with Spring Security, you have to define the "delegatingFilterProxy" filter in web.xml but the JavaConfig equivalent does this behind the scenes. Using Spring Security annotations triggers that behavior.
I recommend two things:
Read through the Spring JavaConfig guide if you haven't done so
If you're stuck on a particular element, search for " JavaConfig". Usually, you can find some useful information quickly that way.
For the welcome page follow these steps:
Under your application root, create a html file called index.html
In that declare a head section with the content: <meta http-equiv="Refresh" content="0; URL=anonymous/homepage.htm"/>
Now when you access your application using http://myapplication.com you will be automatically redirected to http://myapplication.com/anonymous/homepage.htm. This acts as your index/welcome page
For the error page follow these steps:
In the controller:
try
{
}
catch (Exception ex)
{
return new ModelAndView("error");
}
In views.properties (or equivalent) define a error page like:
error.(class)=org.springframework.web.servlet.view.JstlView
error.url=/WEB-INF/jsp/errorpage.jsp

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 ... :)

Display Spring MVC model attributes in thymeleaf template

I am developing a full Spring application with Spring MVC and Thymeleaf in view layer. In the past I've worked with JSPs and Spring MVC in view layer, but those are now dinosaurs I guess.
So my problem is that with JSPs I could very easily display model attributes in view by adding value in model.addAttribute in controller and displaying the same in JSP anywhere with placeholder evaluating to springex ${value}. So if I want to place a title in page I can write <title>${appName}<title>. This is one of the places where I can put any springex.
I am having hard time to figure out how to do this with Thymeleaf as it uses attribute based parsers. So anywhere on page if thymeleaf prefix is not included it won't process spring expression. It's very hard to work with limited set of tag libraries. I've heard of custom attributes for thymeleaf but I guess there should be a better way to do this.
You can use the th:text attribute, e.g.
<html ... xmlns:th="http://www.thymeleaf.org">
...
<title th:text="${appName}">mocking text</title>
...
</html>
The content of the tag ("mocking text" in this case) gets replaced by the result of the expression in the th:text attribute.
Of course you need to have the appropriate JAR files on CLASSPATH and have the Thymeleaf view resolver properly configured, as described in the Thymeleaf+Spring guide.
For additional information about how template processing works with Thymeleaf in general you can refer to the Thymeleaf guide.

Resources