Spring-MVC:重定向到主页,不管它是什么链接,

j7dteeu8  于 2023-05-07  发布在  Spring
关注(0)|答案(4)|浏览(174)

我们有一个维护spring-MVC应用程序,当请求域名时,它只显示一个图像,说明我们正在维护。有没有什么办法,将所有请求的URL路由到'/',因为目前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 Web服务器进行负载平衡只是为了显示维护映像对于输出来说太麻烦了。有什么想法。谢谢。

pvcm50d1

pvcm50d11#

为了在你的控制器中接受所有的url,你可以在方法级使用
@RequestMapping(value ="/**")

au9on6nz

au9on6nz2#

你可以这样做。假设您有两个Web应用程序,上下文不同,一个在外部可见,一个在管理员处保存时不可见。给予管理员一些设置,使应用程序处于维护状态。然后在“公共”应用程序上你可以这样做,这是一个多一点的工作,但给你更多的控制。你甚至可以锁定一些页面,基于请求的URL(你可以通过request.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");
        }
        }
eivgtgni

eivgtgni3#

我相信正确的方法是在web.xml中定义

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

ws51t4hk4#

你可以通过两种方式来做到这一点:

第一种方法:在Spring MVC中使用GlobalExceptionContoller

默认情况下,当DispatcherServlet无法找到请求的处理程序时,它会发送404响应。然而,如果其属性“throwExceptionIfNoHandlerFound”被设置为true,则会引发此异常,并且可以使用配置的HandlerExceptionResolver来处理。

@ControllerAdvice
public class GlobalExceptionContoller {

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

}

**第二种方法:使用错误代码在web.xml文件中设置:404

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

相关问题