Tag: http status code 404

处理Spring中的404错误?

这是我将未映射的请求重定向到404页面的代码 @RequestMapping(“/**”) public ModelAndView redirect() { ModelAndView mv = new ModelAndView(); mv.setViewName(“errorPage”); return mv; } 上面的代码工作正常,但问题是像css和js文件这样的web资源也进入这个重定向方法,并且它没有加载任何文件。 但我已经在我的调度程序servlet中有这个代码,但是spring控制器没有识别这个资源映射。 所以我在请求映射中尝试了一些正则表达式来否定资源url这样的东西 @RequestMapping(“/{^(?!.*resources/**)}**”) public ModelAndView redirect() { ModelAndView mv = new ModelAndView(); mv.setViewName(“errorPage”); return mv; } 但这并没有按预期工作。所以,如果有人能帮助它会很棒:)

防止spring-security将无效URL重定向到登录页面

我已经设置了一个spring-boot + spring-mvc + spring-security项目。 除了无效的url外,一切都按预期正常工作。 如果我发出: http://siteaddr.com/invalid/url 我希望看到我的404找不到页面,但是spring-security首先重定向到登录页面,并且在身份validation后显示404找不到! 我认为这不应该如何运作! 这是我的Websecurity配置: package com.webitalkie.webapp.config; import java.util.EnumSet; import javax.servlet.DispatcherType; import javax.servlet.ServletContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; import com.webitalkie.webapp.security.DatabaseUserDetailsServic; @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private DatabaseUserDetailsServic userDetailsService; @Autowired private ServletContext servletContext; @Override protected void configure(HttpSecurity […]

HOWTO使用基于Java的Annotations配置的Spring MVC全局处理404exception

(问题末尾发布的解决方案 ) 我正在构建一个Spring 4 MVC应用程序。 它完全使用Java Annotations进行配置。 没有web.xml 。 通过使用AbstractAnnotationConfigDispatcherServletInitializer和WebMvcConfigurerAdapter实例配置应用程序, WebMvcConfigurerAdapter所示, @Configuration @EnableWebMvc @ComponentScan(basePackages = {“com.example.*”}) @EnableTransactionManagement @PropertySource(“/WEB-INF/properties/application.properties”) public class WebAppConfig extends WebMvcConfigurerAdapter { … } 和 public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { … } 我现在正在尝试为404页面添加一个global / catch-allexception处理程序,即HttpStatus.NOT_FOUND但没有成功。 以下是我尝试过的一些方法。 import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.NoHandlerFoundException; import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException; @ControllerAdvice public […]

尝试运行servlet时出现HTTP状态404错误

我有一个问题,我正在尝试运行我的简单servlet,Hello.java。 我在eclipse中创建它,然后将文件放在webapps/ServletTest/WEB-INF/classes文件夹中并编译它,在同一文件夹中创建文件Hello.class 。 然后,我修改了我的web.xml文件以映射servlet,并尝试通过以下地址运行它 http://localhost:8080/ServletTest/Hello 但是,这不起作用,给出以下错误 HTTP状态404 – 类型状态报告 信息 description请求的资源不可用。 Apache Tomcat / 7.0.42 web.xml文件中的映射如下所示: Hello Main.Hello Hello /Hello servlet的代码: package Main; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(“/Hello”) public class Hello extends HttpServlet { private static final long serialVersionUID = 1L; public Hello() { super(); […]

Struts 2 – HTTP状态404 – 没有为操作定义结果

我正在尝试开发一个Struts2应用程序,其中在单击超链接时调用操作,该超链接使用Struts动作映射将用户指向hello.jsp。 我收到以下错误: HTTP Status 404 – No result defined for action com.manaar.action.HelloAction and result success 我的文件如下。 我的映射看起来像是按顺序。 我还在这里检查了其他post,但似乎无法找到这个问题的原因或解决方案。 非常感谢任何建议。 非常感谢,J index.jsp : Struts 2 Actions Welcome ! | Logout | Hello Action Add User View Users Login struts.xml : /hello.jsp HelloAction.java : /* * To change this template, choose Tools | Templates * and open […]

Spring MVC配置url-pattern

我尝试配置简单的控制器。 我有: 在web.xml中 mvc-dispatcher org.springframework.web.servlet.DispatcherServlet 1 mvc-dispatcher /index.jsp 在mvc-dispatcher-servlet.xml中 /jsp/ .jsp 这是我的控制器 : public class TBController extends AbstractController { @Override protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { System.out.println(“It is here”); ModelAndView model = new ModelAndView(“index”); return model; }} 我在Tomcat 6上运行,在这个配置中它(/index.jsp)运行完美! 但如果我像这样更改url-pattern mvc-dispatcher *.jsp 它返回404访问/index.jsp 我在控制台中看到“它在这里”,这意味着url-pattern工作正常,但ModelAndView没有初始化 奇怪的是,看起来他试图访问空资源(Chrome让我发现“HTTP状态404 – ”) 请帮助我了解发生了什么..可能是我错过了url-pattern规范中的一些内容? UPD: 感谢Pavel Horal,已找到解决方案。 我刚用web.xml替换了我的url-pattern […]

Tomcat 404错误:原始服务器没有找到目标资源的当前表示,或者不愿意透露存在一个

我正在关注o7planning的教程,并在第6步陷入困境: http://o7planning.org/en/10169/java-servlet-tutorial 这只是一个显示HelloWorld的简单项目,但由于某种原因,我一直收到404错误。 详情: 但是Tomcat欢迎页面正确显示。 这是迄今为止我尝试过的解决方案(并且它们不起作用): 右键单击项目 – >属性 – >项目构面 – >运行时 – >选中“Apache Tomcat v9.0” – >应用 – >完成。 “服务器”选项卡 – >右键单击“Tomcat v9.0 …” – >属性 – >切换位置 – >在“服务器位置”面板上选择“使用tomcat安装”。

Spring Java Config vs Jboss 7

我试图在jboss上运行一个基于spring java的配置的简单应用程序,但没有成功。 这个应用程序在jetty和tomcat上都可以正常工作。 jboss日志看起来不错,因为它向我展示了一些成功的映射等,但我得到404试图访问该URL。 这是我的代码: 初始化 @Order(1) public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class[] getRootConfigClasses() { return new Class[] {RootConfig.class}; } @Override protected Class[] getServletConfigClasses() { return new Class[] {WebAppConfig.class}; } @Override protected String[] getServletMappings() { return new String[] {“/”}; } @Override protected void customizeRegistration(ServletRegistration.Dynamic registration) { registration.setInitParameter(“dispatchOptionsRequest”, “true”); } } RootConfig @Configuration […]