Spring的MVC:重定向到首页,不管它是什么环节。(Spring-MVC : Redirecti

2019-10-23 03:52发布

我们有一个维护弹簧MVC应用程序,它只是显示一个图像,当域名请求说,我们是在维护。 有没有一些方法,以将所有URL的请求“/”在404目前domainname.de/something结果。

控制器代码:

@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>

我们决定创建一个新的小保养项目本身通过Apache网络服务器负载均衡只是为了显示维护形象是对于输出太多的麻烦。 有任何想法吗。 谢谢。

Answer 1:

为了接受您的控制器的所有URL,你可以在方法级别使用

@RequestMapping(value ="/**")



Answer 2:

welll你可以做这样的..假设你有两个web应用,不同的背景下,一个可见的外,一个保存从管理不可见..给管理员一些设置把应用程序正在维护中。 然后在“公共”应用程序,你可以做到这一点,这是更多的工作,但给你更多的控制..你甚至可以锁定只是一些网页,根据请求的URL(您可以通过请求得到它。 getRequestURL()。toString()方法),你让请求传递或重定向到一个404或任何网页..

第一部分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>

第二部分中的应用

       @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");
        }
        }


Answer 3:

我认为正确的方法将是在web.xml中定义

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


文章来源: Spring-MVC : Redirecting to home page no matter what link it is.