We are Borg
We are Borg

Reputation: 5313

Spring-MVC : Redirecting to home page no matter what link it is.

We have a maintenance spring-MVC application which just shows one image when domain name is requested saying we are in maintenance. Is there some way, to route all the url's requested to '/' as currently domainname.de/something results in 404.

Controller code :

@Controller
public class MaintenanceController {

        @RequestMapping(value ="/")
        public String loadMaintenancePage(Model model){
            return "maintenance";
        }
}

web.xml :

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>contextAttribute</param-name>
            <param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>403Jsp</servlet-name>
        <jsp-file>/WEB-INF/views/error/403.jsp</jsp-file>
    </servlet>
    <servlet-mapping>
        <servlet-name>403Jsp</servlet-name>
        <url-pattern>/403</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
    </listener>
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml,/WEB-INF/spring/appServlet/security-applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.png</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.jpg</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
       <filter-name>ExpiresFilter</filter-name>
       <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
       <init-param>
            <param-name>ExpiresByType text/html</param-name>
            <param-value>access plus 1 seconds</param-value>
       </init-param>
       <init-param>
          <param-name>ExpiresByType image</param-name>
          <param-value>access plus 10 weeks</param-value>
       </init-param>

       <init-param>
          <param-name>ExpiresByType font/truetype</param-name>
          <param-value>access plus 10 weeks</param-value>
       </init-param>
       <init-param>
          <param-name>ExpiresByType font/opentype</param-name>
          <param-value>access plus 10 weeks</param-value>
       </init-param>
       <init-param>
          <param-name>ExpiresByType application/x-font-woff</param-name>
          <param-value>access plus 10 weeks</param-value>
       </init-param>
        <init-param>
          <param-name>ExpiresByType application/vnd.ms-fontobject</param-name>
          <param-value>access plus 10 weeks</param-value>
       </init-param>
       <init-param>
          <param-name>ExpiresByType image/svg+xml</param-name>
          <param-value>access plus 10 weeks</param-value>
       </init-param>
    </filter>
    <filter-mapping>
       <filter-name>ExpiresFilter</filter-name>
       <url-pattern>/*</url-pattern>
       <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

We decided to create a new small maintenance project itself as loadbalancing via Apache webserver just to show a maintenance image was too much hassle for the output. Any ideas. Thanks.

Upvotes: 1

Views: 3918

Answers (4)

Du-Lacoste
Du-Lacoste

Reputation: 12777

You can do this by two ways:

First Method: Use GlobalExceptionContoller in Spring MVC

By default, when the DispatcherServlet can't find a handler for a request it sends a 404 response. However, if its property "throwExceptionIfNoHandlerFound" is set to true this exception is raised and may be handled with a configured HandlerExceptionResolver.

@ControllerAdvice
public class GlobalExceptionContoller {

    @ExceptionHandler(NoHandlerFoundException.class)
    public String acceptNotFoundError(HttpServletRequest request) {
        return "display404ErrorPage"; //view the error related JSP
    }

}

Second Method: Set in web.xml file using the error code: 404

<error-page>
        <error-code>404</error-code>
        <location>display404ErrorPage.jsp</location>
</error-page>

Upvotes: 0

witchedwiz
witchedwiz

Reputation: 305

welll you can do it like this.. suppose you have two webapplication, different context, one visible outside, one not visible save from admin.. give admin some setting to put the application under maintenance. then on the "public" application you can do this, which is a bit more work, but gives you much more control.. you can even lock just some pages, based on the url of the request (you can get it via request.getRequestURL().toString()) you let the request pass or redirect it to a 404 or whatever page..

FIRST PART ON web.xml

    <!--  Check Maintenance Filter -->
    <filter>
      <filter-name>checkAvailabilityFilter</filter-name>
      <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>checkAvailabilityFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

SECOND PART ON APPLICATION

       @Component(value = "checkAvailabilityFilter")
       public class CheckAvailabilityFilter extends OncePerRequestFilter{


        @Autowired
        private SomeConfigurationRepository confRepository;


        private static final Logger logger = LoggerFactory.getLogger(CheckAvailabilityFilter.class);

        @Override
        protected void doFilterInternal(HttpServletRequest request,
                HttpServletResponse response, FilterChain filterChain)
                        throws ServletException, IOException {
        if (confRepository.findOne(1L).getUnderMaintenance() == true)
        {
         //or whatever maintenance page you want to display
        response.sendRedirect(request.getContextPath()+"/404");
        }
        }

Upvotes: 1

Fran Montero
Fran Montero

Reputation: 1680

In order to accept all urls in your controller you can use at method level

@RequestMapping(value ="/**")

Upvotes: 2

Stan
Stan

Reputation: 1430

I believe correct way will be to define in web.xml

<error-page>
    <error-code>404</error-code>
    <location>/</location>
</error-page>

Upvotes: 0

Related Questions