SpringMvc4.2.5 零配置出现 No mapping found for HTTP request with URI(转)
原文地址:SpringMvc4.2.5 零配置出现 No mapping found for HTTP request with URI
采用 spring 零配置,参考 http://hanqunfeng.iteye.com/blog/2113820,WebInitializer 类代码如下:
public class WebInitializer implements WebApplicationInitializer { private static final Logger logger = LogManager.getLogger(WebInitializer.class); @Override
public void onStartup(ServletContext servletContext) throws ServletException { logger.info("-------->启动web应用"); /*
* Log4jConfigListener: log4j 2日志配置,配置文件为 classpath:log4j2.xml
*/
servletContext.addListener(new Log4jServletContextListener()); Log4jServletFilter log4jServletFilter = new Log4jServletFilter();
servletContext.addFilter("log4jServletFilter", log4jServletFilter); /*
* OpenSessionInViewFilter: 加上该 filter 可以保证jackson能够序列化LAZY FetchType
*/
OpenSessionInViewFilter openSessionInViewFilter = new OpenSessionInViewFilter();
FilterRegistration.Dynamic openSessionInViewFilterRegistration = servletContext
.addFilter("openSessionInViewFilter", openSessionInViewFilter);
//指定在spring配置的sessionFactory
openSessionInViewFilterRegistration.setInitParameter("sessionFactoryBeanName", "sessionFactory");
//singleSession默认为true,若设为false则等于没用OpenSessionInView
openSessionInViewFilterRegistration.setInitParameter("singleSession", "true");
openSessionInViewFilterRegistration.addMappingForUrlPatterns(
EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE), false, "/"); ServerParameter params = ServerParameter.getInstance();
DBMananger.loadEnv();
if(DBMananger.connDB()) { // 数据库连接成功,再载入APP配置
params.addAttribute("connDBSataus", true);
/*
* spring mvc 外的配置 AppConfig类
*/
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(AppConfig.class);
servletContext.addListener(new ContextLoaderListener(rootContext)); } /*
* spring-mvc 上下文
*/
AnnotationConfigWebApplicationContext springMvcContext = new AnnotationConfigWebApplicationContext();
// springMvcContext.register(MvcConfig.class); /*
* DispatcherServlet
*/
DispatcherServlet dispatcherServlet = new DispatcherServlet(springMvcContext);
ServletRegistration.Dynamic dynamic = servletContext.addServlet("dispatcherServlet", dispatcherServlet);
dynamic.setLoadOnStartup(1);
dynamic.addMapping("/"); /*
* CharacterEncodingFilter: 解决中文乱码问题
*/
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("utf-8");
FilterRegistration characterEncodingfilterRegistration =
servletContext.addFilter("characterEncodingFilter", characterEncodingFilter);
characterEncodingfilterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
} }
MvcConfig 配置如下:
public class MvcConfig extends WebMvcConfigurationSupport { private static final Logger logger = LogManager.getLogger(MvcConfig.class); /**
* 注册视图处理器
* @return
*/
@Bean
public ViewResolver viewResolver() {
logger.info("注册视图处理器-------->ViewResolver");
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
} /**
* 资源访问处理器
* 可以在jsp中使用/images/**的方式访问/images/下的内容
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
logger.info("资源访问处理器-------->addResourceHandlers");
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/")
.resourceChain(true).addResolver(
new VersionResourceResolver().addContentVersionStrategy("/**"));
}
}
结果出现No mapping found for HTTP request with URI警告,访问静态资源时为404错误。
经调试发现,在MvcConfig类中加入下面Bean,问题解决
@Bean
public HandlerMapping resourceHandlerMapping() {
logger.info("HandlerMapping");
return super.resourceHandlerMapping();
}
为什么要显式声明该Bean,尚未得知。
另外,如果在MvcConfig类中加注解,@Configuration,在WebInitializer类中配置
AnnotationConfigWebApplicationContext springMvcContext = new AnnotationConfigWebApplicationContext();
springMvcContext.register(MvcConfig.class);
将会导致MvcConfig加载两次,可以注释掉其中一边。
SpringMvc4.2.5 零配置出现 No mapping found for HTTP request with URI(转)的更多相关文章
- spring访问静态资源出错,No mapping found for HTTP request with URI xxx/resources/js/jquery.min.js...
问题:spring访问静态资源出错,No mapping found for HTTP request with URI xxx/resources/js/jquery.min.js... web.x ...
- springmvc No mapping found for HTTP request with URI in Dispatc
springmvc No mapping found for HTTP request with URI in Dispatc 博客分类: Java Web springmvcspring MVCNo ...
- SpringMvc出现No mapping found for HTTP request with URI的终极解决办法
No mapping found for HTTP request with URI 出现这个问题的原因是在web.xml中配置错了,如: <servlet> <servlet-na ...
- 相对路径获取项目文件 及报错 No mapping found for HTTP request with URI XXX in DispatcherServlet with name ‘springmvc’解决方法
首先一点,WebRoot目录下的文件是都可以通过浏览器输入路径,直接读取到的 例如这样: 而WebRoot下面WEB-INF是无法浏览器输入路径直接读取的. 因为是受保护的. 如果jsp读取一个图片的 ...
- org.springframework.web.servlet.PageNotFound No mapping found for HTTP request with URI [/AssetRepair/assetRepairController/test.do] in DispatcherServlet with name 'assetrepair'
web.xml文件配置: xxx-servlet.xml 我们可以发现DispatcherServlet会处理"jsp"后缀的请求;而模型视图后缀也是jsp的 如果这样配置会报以下 ...
- Spring3中js/css/jpg/gif等静态资源无法找到(No mapping found for HTTP request with URI)问题解决--转载
原文地址:http://www.programgo.com/article/96083031845/ 最近项目中使用到Spring3,在感叹Spring3注解配置清爽的同时竟然出现了这个不和谐的事情, ...
- No mapping found for HTTP request with URI [/user/login.do] in DispatcherServlet with name 'dispatcher'错误
1.警告的相关信息 七月 24, 2017 3:53:04 下午 org.springframework.web.servlet.DispatcherServlet noHandlerFound警告: ...
- <context:annotation-config/>和<mvc:annotation-driven/>及解决No mapping found for HTTP request with URI [/role/getRole] in DispatcherServlet with name 'springmvc-config'
1:什么时候使用<context:annotation-config> 当你使用@Autowired,@Required,@Resource,@PostConstruct,@PreDest ...
- org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI
在重启项目中会遇到[org.springframework.web.servlet.PageNotFound] - No mapping found for HTTP request with URI ...
随机推荐
- 【Python】一些练习代码用的图片
- hdu 3861 The King’s Problem trajan缩点+二分图匹配
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- JDBC Connection Configuration
如果在Jmeter 中想用到连接数据库的功能,必须下载jar包,常见的关系型数据库jar包见以下共享链接 链接:https://pan.baidu.com/s/1t-k9RW141lw0j_QSw53 ...
- Java SE 枚举的基本用法
出于对自己基础的稳打,期末考试后依旧对SE部分进行复习 枚举的基本用法 public enum Season { SPRING,SUMMER,AUTUMN,WINTER } public class ...
- CSS段落对齐方式
CSS段落对齐有两种方式:水平对齐和垂直对齐. 1.水平对齐: (1).text-align:left; //左对齐 (2).text-align:right; //右对齐 ...
- @Scope注解设置创建bean的方式和生命周期
1.1.1 Scope注解创建bean的方式和生命周期 作用 Scope设置对象在spring容器(IOC容器)中的生命周期,也可以理解为对象在spring容器中的创建方式. 取 ...
- 完全二叉树的节点个数 Count Complete Tree Nodes
2018-09-25 16:36:25 问题描述: 问题求解: 单纯遍历了一遍,emmm,果然TLE. 解题思路就是比较左边树高度和右边树高度,如果相等,那么就是一个满二叉树,返回1 << ...
- DELPHI各种颜色表达式
颜色样本 十六进制 名称与注释 #ffb3a7 粉红:即浅红色.别称:妃色 杨妃色 湘妃色 妃红色. #ed5736 妃色:妃红色.古同“绯”,粉红色.杨妃色.湘妃色.粉红皆同义. #f0 ...
- 记 linux 下面初次使用的convert 工具完成拼长图功能
今天,遇到了一个需要把大量图片合并到一个长图的功能.本来找了各种图片处理界顶顶大佬.. “PS(手动一张张 的加).光影魔术手(批处理功能没有看到拼图功能).美图秀秀(可以有个拼图,限制30张,而且需 ...
- webService上传图片
webService /// <summary> /// 上传图片webServer 的摘要说明 /// </summary> [WebService(Namespace = ...