簡體   English   中英

Spring-MVC:重定向到主頁,無論它是什么鏈接。

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

我們有一個維護spring-MVC應用程序,當請求域名時我們正在維護時只顯示一個圖像。 是否有某種方法,將所有請求的網址路由到'/',因為當前domainname.de/something會導致404。

控制器代碼:

@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 webserver創建一個新的小型維護項目作為負載平衡,只是為了顯示維護圖像對輸出來說太麻煩了。 有任何想法嗎。 謝謝。

為了接受控制器中的所有URL,您可以在方法級別使用

@RequestMapping(value ="/**")

你可以這樣做..假設你有兩個webapplication,不同的上下文,一個可見的外部,一個不可見從管理員保存..給管理員一些設置,使應用程序在維護。 然后在“公共”應用程序上你可以做到這一點,這是一個更多的工作,但給你更多的控制..你甚至可以根據請求的網址鎖定一些頁面(你可以通過請求獲得它。 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");
        }
        }

我相信正確的方法是在web.xml中定義

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM