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

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

Related

Spring: How can I keep routes in sync with URLs on the page?

How can I keep links in my UI templates (e.g. Thymeleaf templates) in sync with the corresponding request mappings in my Spring application?
I've seen that e.g. the Play framework uses the #router-Object within its templates. How is it solved by Spring?
One example:
Spring Controller - simple
#Controller
public class UserController {
#GetMapping("/users/{username}")
public String getUser(#PathParam String username) {
// do some stuff....
return "user";
}
}
HTML-Page
<body>
User details
</body>
Now I want to change "/users" to "/accounts". I'm pretty sure that I've got to update every html page by hand to update the link. Is there an easier solution for this?
As far as I know, there is no simple way to do this with built-in tools from Spring. However, I don't think that this would be too hard to build. You would need the following:
A YAML file with all of your URL templates defined
A Properties Spring bean that contains your URL mappings (read from the YAML file)
All of your #RequestMapping annotations would have to be prop values; i.e.
#GetMapping("${urls.users.byUsername}")
A custom tag that knows about the Properties bean and can create URLs from the templates that were defined in your YAML file.

Why does Spring Boot replace all of my favicons with Spring's leaf icon?

(I examined similar questions, but none of them explain the odd behavior I illustrate at the end of this question.)
I have a Spring Boot 1.3.5 application that insists on replacing my favicon with Boot's default favicon (the green leaf). To resolve the problem, I have tried the following:
Install my own favicon at my application's static root.
The word on the street is that this is just supposed to work. Unfortunately, it does not.
Set property spring​.​mvc​.​favicon​.​enabled to false.
This is supposed to disable org​.​springframework​.​boot​.​autoconfigure​.​web​.​WebMvcAutoConfiguration​.​WebMvcAutoConfigurationAdapter​.​FaviconConfiguration, which appears to responsible for serving Boot's default favicon. By setting a breakpoint in that class, I was able to confirm that the beans defined in that class indeed do not get created when the property is set to false.
Unfortunately, this didn't solve the problem, either.
Implement my own favicon handler:
#Configuration
public class FaviconConfiguration {
#Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Integer.MIN_VALUE);
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", faviconRequestHandler()));
return mapping;
}
#Bean
protected ResourceHttpRequestHandler faviconRequestHandler() {
ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
ClassPathResource classPathResource = new ClassPathResource("static/");
List<Resource> locations = Arrays.asList(classPathResource);
requestHandler.setLocations(locations);
return requestHandler;
}
}
Sadly, no luck here, too.
Rename my favicon from favicon.ico to logo.ico, and just point all my pages' favicon links to that, instead.
Now, with this potential fix, I discovered a surprising result. When I curled my newly named icon.ico resource, I was served Spring's leaf icon. However, when I deleted the resource, I got 404. But then, when I put it back, I got the leaf again! In other words, Spring Boot was happy to answer 404 when my static resource was missing, but when it was there, it would always answer with the leaf instead!
Incidentally, other static resources (PNGs, JPGs, etc.) in the same folder serve up just fine.
It was easy to imagine that there was some evil Spring Boot contributor laughing himself silly over this, as I pulled my hair out. :-)
I'm out of ideas. Anyone?
As a last resort, I may just abandon using an ICO file for my site icon, and use a PNG instead, but that comes at a cost (losing multi-resolution support), so I'd rather avoid that.
This is a spring boot feature:
Spring MVC auto-configuration
Spring Boot provides auto-configuration for Spring MVC that works well with most applications.
The auto-configuration adds the following features on top of Spring’s defaults:
Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver
beans.
Support for serving static resources, including support for
WebJars (see below).
Automatic registration of Converter,
GenericConverter, Formatter beans.
Support for HttpMessageConverters
(see below).
Automatic registration of MessageCodesResolver (see
below).
Static index.html support.
Custom Favicon support.
Automatic use of a ConfigurableWebBindingInitializer bean (see below).
You can find this document at: http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-auto-configuration
And, If you want to disable spring boot favicon, you can add this config to you yml or perperties files
spring.mvc.favicon.enabled=true # Enable resolution of favicon.ico.
Or, If you want change favicon to you own. try this:
#Configuration
public static class FaviconConfiguration {
#Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Integer.MIN_VALUE);
mapping.setUrlMap(Collections.singletonMap("mylocation/favicon.ico",
faviconRequestHandler()));
return mapping;
}
}
And you can find more detail at: Spring Boot: Overriding favicon
UPDATE:
put favicon.ico to resources folder.
And, try it:
Why choose the hard way, when u can get the easy one?
just create a new link into ur <head> with:
<link rel="icon" type="image/png" href="images/fav.png" />
Copy and paste ur icon in src/main/resources/static/images/
Rename the file to whatever you want, just remember to change the link in the html too.

Can <security-constraint> tag in web.xml be dynamically generated or written outside web.xml?

I met a problem, I want to set the tag of security-constraint according to my configuration file dynamically, but I can't do it. So I hope tag in web.xml can be dynamically generated or written outside web.xml. Thanks a lot for your help!
I think your question could be related to this one. However, if you were working with Servlet 3.0 spec, you could try the approach of programmatically adding and configuring security for the servlet, as shown here.

Spring MVC 3.0 - restrict what gets routed through the dispatcher servlet

I want to use Spring MVC 3.0 to build interfaces for AJAX transactions. I want the results to be returned as JSON, but I don't necessarily want the web pages to be built with JSP. I only want requests to the controllers to be intercepted/routed through the DispatcherServlet and the rest of the project to continue to function like a regular Java webapp without Spring integration.
My thought was to define the servlet-mapping url pattern in web.xml as being something like "/controller/*", then have the class level #RequestMapping in my controller to be something like #RequestMapping("/controller/colors"), and finally at the method level, have #RequestMapping(value = "/controller/colors/{name}", method = RequestMethod.GET).
Only problem is, I'm not sure if I need to keep adding "/controller" in all of the RequestMappings and no matter what combo I try, I keep getting 404 requested resource not available errors.
The ultimate goal here is for me to be able to type in a web browser "http://localhost:8080/myproject/controller/colors/red" and get back the RGB value as a JSON string.
You are not correct about needing to add the entire path everywhere, the paths are cumulative-
If you have a servlet mapping of /controller/* for the Spring's DispatcherServlet, then any call to /controller/* will be handled now by the DispatcherServlet, you just have to take care of rest of the path info in your #RequestMapping, so your controller can be
#Controller
#RequestMapping("/colors")
public class MyController{
#RequestMapping("/{name}
public String myMappedMethod(#PathVariable("name") String name, ..){
}
}
So now, this method will be handled by the call to /controller/colors/blue etc.
I don't necessarily want the web pages to be built with JSP
Spring MVC offers many view template integration options, from passthrough to raw html to rich templating engines like Velocity and Freemarker. Perhaps one of those options will fit what you're looking for.

How to develop JSP/Servlets Web App using MVC pattern?

I'm developing a JSP/Servlet web app (no frameworks). I want to use MVC pattern. I am going to design my project like this:
Controller: a servlet that reads a request, extracts the values,communicates with model objects and gives information to a JSP page.
View: JSP Pages.
Model: Java Classes / Java Beans .. etc.
The problem: Index.jsp is the starting point (default page) in my web site. So, the Index.jsp becomes the controller to parse the request. For example, the following request:
index.jsp?section=article&id=10
is parsed in index.jsp as following :
<div class="midcol">
<!-- Which section? -->
<%String fileName = request.getParameter("section");
if (fileName == null) {
fileName = "WEB-INF/jspf/frontpage.jsp";
} else {
fileName = "WEB-INF/jspf/" + fileName + ".jsp";
}
%>
<jsp:include page='<%= fileName%>' />
</div>
Here, I can't force the servlet to be a controller, because the index.jsp is the controller here since it's the starting point.
Is there any solution to forward the request from index.jsp to the servlet and then go back to index.jsp? Or any solution that achieves the MVC goal - the servlet should be the controller?
I'm thinking of making a FrontPageController servlet as default page instead of index.jsp, but I don't know if it's a perfect idea?
Get rid of index.jsp and just let the controller servlet listen on a specific url-pattern of interest. The controller itself should forward the request to the JSP page of interest using RequestDispatcher.
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
Alternatively you can let index.jsp forward or redirect to an URL which is covered by the controller servlet which in turn shows the "default" page (which seems to be frontpage.jsp).
That said, in a correct MVC approach, you should have no scriptlets in JSP files. Whenever you need to write some raw Java code inside a JSP file which can't be replaced reasonably by taglibs (JSTL and so on) or EL, then the particular Java code belongs in any way in a real Java class, like a Servlet, Filter, Javabean, etcetera.
With regard to the homegrown MVC approach, you may find this answer and this article useful as well.

Resources