spring管理事务失效的原因和No mapping found for HTTP request with URI []DispatcherServlet with name 'sprinmvc错误

练习了一个spring的事务配置问题,在搭建spring和springmvc时,考验了一下事务,结果发现事务竟然失效了。这里做一下总结。

一般我们在Spring的配置文件application.xml中对Service层代码配置事务管理,可以对Service的方法进行AOP增强或事务处理如事务回滚,但是遇到一个问题,在Controller类中调用Service层方法,配置的事务管理会失效,查询相关资料发现原因。其实Spring和SpringMVC俩个容器为父子关系,Spring为父容器,而SpringMVC为子容器。也就是说application.xml中应该负责扫描除@Controller的注解如@Service,而SpringMVC的配置文件应该只负责扫描@Controller,否则会产生重复扫描导致Spring容器中配置的事务失效。


因此正确的配置方式应该为:

springmvc的扫描包路径:

<!-- 开启扫描包 -->
 <context:component-scan base-package="com.xiaoma.controller" />
而spring的扫描包路径:

<!-- 开启扫描包 -->
<context:component-scan base-package="com.xiaoma.service" />
<context:component-scan base-package="com.xiaoma.dao" />
之后无论是声明式事务:还是注解式事务都没有问题了!

特别注意:

扫描包如果是分开了写的话必须具体到某个包下不能写成如下的样子,不然访问结果就是404.

<!-- 开启扫描包 -->
<context:component-scan base-package="com.xiaoma.controller.*" />
而异常就是:

No mapping found for HTTP request with URI [/springFramework1/User/user] in DispatcherServlet with name 'springmvc'


No mapping found for HTTP request with URI in DispatcherServlet

03-21

在做一个springMVC项目的时候,遇到了这种问题。在网上看了好多总是无法解决。rnweb.xml代码如下:rn[code=html]rnrn mvcrn rn index.htmlrn index.htmrn index.jsprn default.htmlrn default.htmrn default.jsprn rn rn springMVCrn org.springframework.web.servlet.DispatcherServletrn rn contextConfigLocationrn classpath*:config/springAnnotation-servlet.xmlrn rn 1rn rn rn rn springMVCrn /rn rn [/code]rnrnspringAnnotation-servlet.xml代码如下:rn[code=html]rnrn rn rn rn rn rn rn rn rn rn rnrn rn rn rn rn [/code]rnrnUserController.java代码如下:rn[code=java]package com.controller.annotation;rnrnimport org.springframework.stereotype.Controller;rnimport org.springframework.web.bind.annotation.RequestMapping;rnimport org.springframework.web.bind.annotation.RequestMethod;rnimport org.springframework.web.servlet.ModelAndView;rnrn@Controllerrnpublic class UserController rn rn @RequestMapping(value="/user/addUser",method=RequestMethod.POST)rn public ModelAndView addUser()rn rn String result ="this is addUser------";rn return new ModelAndView("/annotation","result",result);rn rn rn @RequestMapping(value="/user/delUser",method=RequestMethod.GET)rn public ModelAndView delUser()rn String result ="this is delUser------";rn return new ModelAndView("/annotation","result",result);rn rn @RequestMapping(value="/user/toUser",method=RequestMethod.GET)rn public ModelAndView toUser()rn return new ModelAndView("/annotation");rn rnrn[/code]rnrnannotation.jsp代码如下:rn[code=html]<%@ page language="java" contentType="text/html; charset=UTF-8"rn pageEncoding="UTF-8"%>rn<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>rnrnrnrn rn rn Insert title herernrnrnrn rn springMVC注解1rn rn rn $result rn rn rn rnrn[/code]rn通过URL访问时总是提示警告: No mapping found for HTTP request with URI [/mvc4/user/addUser] in DispatcherServlet with name 'springMVC'

求助:Spring MVC环境报错 No mapping found for HTTP request with URI

08-10

之前一直用的struts2,第一次自己搭建一个简单的spring mvc架构,一直卡在这个报错rnorg.springframework.web.servlet.PageNotFound noHandlerFoundrn警告: No mapping found for HTTP request with URI [/yplatform/version.htm] in DispatcherServlet rnwith name 'yplatform'。rn其实是一个很简单的Demo,已经找了一天问题了,只能发帖求助。我贴下我的配置,rn先是项目结构rn[img=http://img.bbs.csdn.net/upload/201508/10/1439187340_600288.png][/img]rnrn然后是web.xml配置rn[img=http://img.bbs.csdn.net/upload/201508/10/1439187435_691567.png][/img]rnrn再是spring-mvc.xmlrn[img=http://img.bbs.csdn.net/upload/201508/10/1439187610_308829.png][/img]rnrn最后看下Controller里的配置rn[img=http://img.bbs.csdn.net/upload/201508/10/1439187838_78101.png][/img]rnrnspring-config里没有什么配置,都放到spring-mvc.xml里了,之前也放过spring-config,然并卵。rn百度过很多帖子,有3/4都说是吧Url-pattern从/*改成/即可,或者*.do 类似这样的,这些原理我都懂rn但是现在并没有涉及到其他东西,只是一个简单的请求跳转,打了断点并没有进方法,rn怀疑是不是这个controller没有被扫描到,控制台一直是这个报错信息rn[img=http://img.bbs.csdn.net/upload/201508/10/1439187939_738032.png][/img]rnrn望各位有经验的前辈不吝赐教,有什么其他信息需要提供的请补充。rn

spring hello 基础案例 报错(No mapping found for HTTP request with URI [/hello] )

05-31

rn[color=#800000]web的配置:[/color]rn[code=java]rnrnrn rn rn contextConfigLocationrn classpath*:spring/applicationContext.xml rn rn rn org.springframework.web.context.ContextLoaderListenerrn rn rn rn rn rn springMVCrn org.springframework.web.servlet.DispatcherServletrn rn contextConfigLocationrn classpath*:spring/springMVCrn rn 1rn rn rn springMVCrn /rn rn rn rn index.htmlrn index.htmrn index.jsprn default.htmlrn default.htmrn default.jsprn rnrnrn[/code]rn[color=#800000]springmvc的配置:[/color]rn[code=java]rnrnrnrn rn rn rn rn rn rn rn rn rn rnrn[/code]rn[color=#800000]action的代码:[/color]rn[code=java]rnpackage com.exercise.cn.action;rnrnrnimport javax.servlet.http.HttpServletRequest;rnimport javax.servlet.http.HttpServletResponse;rnrnimport org.springframework.context.ApplicationContext;rnimport org.springframework.context.support.ClassPathXmlApplicationContext;rnimport org.springframework.web.servlet.ModelAndView;rnimport org.springframework.web.servlet.mvc.Controller;rnrnimport com.exercise.cn.entity.User;rnimport com.exercise.cn.biz.impl.IuserBiz;rnrnpublic class HelloAction implements Controllerrn rn @Overridern public ModelAndView handleRequest(HttpServletRequest arg0,rn HttpServletResponse arg1) throws Exception rn //1、收集参数、验证参数 rn //2、绑定参数到命令对象 rn //3、将命令对象传入业务对象进行业务处理 rn //4、选择下一个页面 rn ModelAndView mv = new ModelAndView(); rn //添加模型数据 可以是任意的POJO对象 rn mv.addObject("message", "Hello World!"); rn //设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面 rn mv.setViewName("hello"); rn System.out.println("hello");rn return mv; rn rn rn public static void main(String[] args) rn ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");rn IuserBiz b = (IuserBiz) ctx.getBean("userBiz");rn User user = new User();rn b.save(user);rn rnrnrnrn[/code]rnrn[color=#FF0000]错误信息:rn2015-5-31 15:03:58 org.springframework.web.servlet.PageNotFound noHandlerFoundrn警告: No mapping found for HTTP request with URI [/hello] in DispatcherServlet with name 'springMVC'[/color]rn

No mapping found for HTTP request with URI错误,找了一天了也没解决

02-18

刚学spring mvc,按照介绍写的代码,一直报这个错误,component-scan加了,mvc:annotation-driven加了,mvc:default-servlet-handler也加了,就是不行,还请各位前辈帮看下。rnweb.xml配置:rn[code=text]rnrnrnrn Archetype Created Web Applicationrn rn hellospringrn org.springframework.web.servlet.DispatcherServletrn rn contextConfigLocationrn classpath*:WEB-INF/hellospring-config.xmlrn rn 1rn rn rn hellospringrn /rn rnrn[/code]rn[color=#FF0000]hellospring-config.xml[/color]rn[code=text]rnrnrn rn rn rn rn rn rn rn rn rnrn rnrn[/code]rnrnHelloControllerrn[code=java]rnpackage com.hellospring.web.controller;rnrnimport javax.servlet.http.HttpServletRequest;rnimport javax.servlet.http.HttpServletResponse;rnrnimport org.apache.log4j.Logger;rnimport org.springframework.stereotype.Controller;rnimport org.springframework.web.bind.annotation.RequestMapping;rnimport org.springframework.web.servlet.ModelAndView;rnimport org.springframework.web.servlet.mvc.AbstractController;rnrn@Controllerrnpublic class HelloController extends AbstractController rn rn //define logrn private static final Logger logger = Logger.getLogger(HelloController.class.getName());rnrn @Overridern @RequestMapping(value="/hello")rn protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception rn // TODO Auto-generated method stubrn logger.info("hello controller start....");rn ModelAndView mvc = new ModelAndView();rn mvc.addObject("message", "hello spring");rn mvc.setViewName("/WEB-INF/hello/welcome.jsp");rn return mvc;rn rnrnrnrn[/code]

springMVC框架使用的时候出现“警告: No mapping found for HTTP request with URI [/login]”

08-01

我是刚刚学习springMVC这个框架的小白,我部署完之后可以访问到index.jsp,但是在提交表单的时候就报了如题错误,我的各个配置文件如下:rnrnweb.xml:rn[code=html]rnrn Archetype Created Web Applicationrn rn rn rn rn dispatcherrn org.springframework.web.servlet.DispatcherServletrn 1rn rn rn rn dispatcherrn /rn rnrn rn rn encodingFilterrn org.springframework.web.filter.CharacterEncodingFilterrn rn encodingrn UTF-8rn rn rn forceEncodingrn truern rn rn rn encodingFilterrn /*rn rn rn rnrn[/code]rnrndispatcher-servlet.xml:rn[code=html]rnrn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn[/code]rnpom.xml:rn[code=html]rn 4.0.0rn com.lgc.toolsrn html2xmlrn warrn 0.0.1-SNAPSHOTrn html2xml Maven Webapprn http://maven.apache.orgrn rn UTF-8rn 3.1.2.RELEASErn rn rn rn junitrn junitrn 3.8.1rn testrn rn rn rn org.springframeworkrn spring-webmvcrn $spring.versionrn rnrn rn org.springframeworkrn spring-jdbcrn $spring.versionrn rnrn rn org.springframeworkrn spring-contextrn $spring.versionrn rnrn rn org.springframeworkrn spring-aoprn $spring.versionrn rnrn rn org.springframeworkrn spring-corern $spring.versionrn rnrn rn org.springframeworkrn spring-testrn $spring.versionrn rn rn org.springframeworkrn spring-txrn $spring.versionrn rn rn rnrnrn rn html2xmlrn rnrn[/code]rnrnindex.jsp:rn[code=html]<%@ page language="java" contentType="text/html; charset=UTF-8"rn pageEncoding="UTF-8"%>rnrnrnrnrn rn 欢迎rnrnrn Hello World!rnrn rn 用户名: rn 密 码: rn rn rn 当前IP:<%=request.getRemoteAddr()%>rnrnrnrn[/code]rnrnMyController.java:rn[code=java]package com.my.web.controller;rnrnimport org.springframework.stereotype.Controller;rnimport org.springframework.web.bind.annotation.RequestMapping;rnimport org.springframework.web.bind.annotation.RequestParam;rnimport org.springframework.web.bind.annotation.ResponseBody;rnrn/**rn * TODO 控制层代码rn * @author kevinrn * @date 2015年08月01日rn */rn@Controllerrnpublic class MyController rn rn @RequestMapping("login") //用来处理前台的login请求rn private @ResponseBody String hello(rn @RequestParam(value = "username", required = false)String username,rn @RequestParam(value = "password", required = false)String passwordrn )rn return "Hello "+username+",Your password is: "+password;rn rn rnrnrn[/code]rnrnrn搞了一晚上了,都找不出原因,因为没有怎么用过这个论坛,所以分不多,但还是希望有前辈可以帮我解决这个问题。rnrn

SpringMVC出错警告: No mapping found for HTTP request with URI [/SpringMVCTest/user/u

09-23

今天试着接触SpringMVC出错了 求救!!!rn我的 web.xml配置 rn rnrn rn rn springmvcrn org.springframework.web.servlet.DispatcherServletrn rn rn contextConfigLocationrn rn classpath:springmvc.xmlrn rn 1rn rn rn springmvcrn /*rn rnrn rnrn我的springmvc.xml配置rnrnrnrnrn rnrnrnrnrnrnrnrnrnrn rn rn rnrnrnrncom.wen.controller下的类rn@Controllerrn@RequestMapping("/user")rnpublic class UserController rnrn private Map users = new HashMap();rn //默认构造函数rn public UserController()rn users.put("zs", new User("zs", "123", "张三", "123@163.com"));rn users.put("ls", new User("ls", "123", "李四", "234@163.com"));rn users.put("we", new User("we", "123", "王五", "345@163.com"));rn users.put("sj", new User("sj", "123", "世杰", "456@163.com"));rn users.put("yp", new User("yp", "123", "一平", "567@163.com"));rn users.put("ww", new User("ww", "123", "微微", "678@163.com"));rn rnrn @RequestMapping(value="/users", method=RequestMethod.GET)rn public String list(Model model)rn model.addAttribute("users", users);rn return "user/list";rn rnrn //连接到add页面时是GET请求,会访问这段代码rn @RequestMapping(value="/add", method=RequestMethod.GET)rn public String add(Model model)rn //必须添加一个User,不然在add.jsp中rn //modelAttribute="user"的user无法获取rn model.addAttribute(new User());rn //服务器端跳转:自动加上"forward:"rn return "user/add";rn rnrn //除了上面一种方式,我更喜欢下面这种rn //@RequestMapping(value="/add", method=RequestMethod.GET)rn //public String add(@ModelAttribute("user") User user) rn // return "user/add";rn // rnrn //在具体添加用户时,是POST请求,会访问这段代码rn @RequestMapping(value="/add", method=RequestMethod.POST)rn public String add(User user)rn users.put(user.getUsername(), user);rn //客户端跳转:使用"redirect:"rn return "redirect:/user/users";rn rnrnrnlist.jsprn<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>rnrnrnrn rn Insert title herernrnrn 添加用户rn rn rn rn rn $um.value.nickname rn $um.value.password rn $um.value.email rn rn rn rn rnrnrnrnrn每次运行http://127.0.0.1:8080/SpringMVCTest/user/users 总是出现这样的警告rn2016-9-23 15:45:29 org.springframework.web.servlet.PageNotFound noHandlerFoundrn警告: No mapping found for HTTP request with URI [/SpringMVCTest/user/users] in DispatcherServlet with name 'springmvc'rnrn出现HTTP Status 404 -

spring mvc+mybatis+mysql 事务失效

01-06

如题,配置文件如下rnspring_applicationContext.xmlrn[code=java] rnrn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rnrn rn rn[/code]rnrnspringMvc_servlet.xmlrnrn[code=java]rnrn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn [/code]rnrnCaptchaServiceImplrn[code=java]package com.web.service.impl;rnrnimport java.util.Date;rnrnimport javax.annotation.Resource;rnimport javax.servlet.http.HttpServletRequest;rnimport javax.transaction.Transactional;rnrnimport org.springframework.stereotype.Service;rnrnimport net.sf.json.JSONObject;rnrnimport com.web.bean.CaptchaBean;rnimport com.web.dao.CaptchaDao;rnimport com.web.service.CaptchaService;rnimport com.web.utils.MyUtils;rnrn@Servicern@Transactionalrnpublic class CaptchaServiceImpl implements CaptchaService rn @Resourcern private CaptchaDao cDao;rn rn @Overridern public boolean insertCaptcha(HttpServletRequest request) rn //String param=MyUtils.getParam(request);rn String param="\"mobile\":\"15333333333\"";rn JSONObject pjs = JSONObject.fromObject(param); rn String code=MyUtils.createCaptcha();rn String mobile=pjs.getString("mobile");rn //将前面发送的验证码失效rn cDao.updateCaptchaByMoblie(mobile);rn CaptchaBean cb=new CaptchaBean();rn cb.setCaptcha(code);rn //cb.setMoblieNumber(mobile);此处故意让insert报错。但是update没有回滚rn cb.setCreateTime(new Date());rn int i=cDao.insertCaptcha(cb);rn if(i>0)rn return true;rn rn return false;rn rnrnrn[/code]

No mapping found for dependency [type=java.lang.String, name='actionPackages']

08-21

struts2-codebehind包引起了rnjava.lang.RuntimeException: java.lang.RuntimeException: com.opensymphony.xwork2.inject.DependencyException: com.opensymphony.xwork2.inject.ContainerImpl$MissingDependencyException: No mapping found for dependency [type=java.lang.String, name='actionPackages'] in public void org.apache.struts2.config.ClasspathPackageProvider.setActionPackages(java.lang.String).rn at com.opensymphony.xwork2.inject.ContainerBuilder$4.create(ContainerBuilder.java:132)rn at com.opensymphony.xwork2.inject.Scope$2$1.create(Scope.java:51)rn at com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:507)rn at com.opensymphony.xwork2.inject.ContainerImpl$8.call(ContainerImpl.java:540)rn at com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:574)rn at com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:538)rn at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:198)rn at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55)rn at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:360)rn at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:403)rn at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:190)rn at org.mortbay.jetty.servlet.FilterHolder.doStart(FilterHolder.java:97)rn at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)rn at org.mortbay.jetty.servlet.ServletHandler.initialize(ServletHandler.java:620)rn at org.mortbay.jetty.servlet.Context.startContext(Context.java:140)rn at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1233)rn at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:517)rn at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:460)rn at org.mortbay.jetty.plugin.Jetty6PluginWebAppContext.doStart(Jetty6PluginWebAppContext.java:124)rn at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)rn at org.mortbay.jetty.plugin.AbstractJettyRunMojo.restartWebApp(AbstractJettyRunMojo.java:441)rn at org.mortbay.jetty.plugin.AbstractJettyRunMojo$1.filesChanged(AbstractJettyRunMojo.java:402)rn at org.mortbay.util.Scanner.reportBulkChanges(Scanner.java:486)rn at org.mortbay.util.Scanner.reportDifferences(Scanner.java:352)rn at org.mortbay.util.Scanner.scan(Scanner.java:280)rn at org.mortbay.util.Scanner$1.run(Scanner.java:232)rn at java.util.TimerThread.mainLoop(Timer.java:512)rn at java.util.TimerThread.run(Timer.java:462)rnCaused by: java.lang.RuntimeException: com.opensymphony.xwork2.inject.DependencyException: com.opensymphony.xwork2.inject.ContainerImpl$MissingDependencyException: No mapping found for dependency [type=java.lang.String, name='actionPackages'] in public void org.apache.struts2.config.ClasspathPackageProvider.setActionPackages(java.lang.String).rn at com.opensymphony.xwork2.inject.ContainerImpl.inject(ContainerImpl.java:495)rn at com.opensymphony.xwork2.inject.ContainerImpl$7.call(ContainerImpl.java:532)rn at com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:581)rn at com.opensymphony.xwork2.inject.ContainerImpl.inject(ContainerImpl.java:530)rn at com.opensymphony.xwork2.config.impl.LocatableFactory.create(LocatableFactory.java:32)rn at com.opensymphony.xwork2.inject.ContainerBuilder$4.create(ContainerBuilder.java:130)rn ... 27 morernCaused by: com.opensymphony.xwork2.inject.DependencyException: com.opensymphony.xwork2.inject.ContainerImpl$MissingDependencyException: No mapping found for dependency [type=java.lang.String, name='actionPackages'] in public void org.apache.struts2.config.ClasspathPackageProvider.setActionPackages(java.lang.String).rn at com.opensymphony.xwork2.inject.ContainerImpl.addInjectorsForMembers(ContainerImpl.java:144)rn at com.opensymphony.xwork2.inject.ContainerImpl.addInjectorsForMethods(ContainerImpl.java:113)rn at com.opensymphony.xwork2.inject.ContainerImpl.addInjectors(ContainerImpl.java:90)rn at com.opensymphony.xwork2.inject.ContainerImpl$1.create(ContainerImpl.java:71)rn at com.opensymphony.xwork2.inject.ContainerImpl$1.create(ContainerImpl.java:69)rn at com.opensymphony.xwork2.inject.util.ReferenceCache$CallableCreate.call(ReferenceCache.java:150)rn at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)rn at java.util.concurrent.FutureTask.run(FutureTask.java:138)rn at com.opensymphony.xwork2.inject.util.ReferenceCache.internalCreate(ReferenceCache.java:76)rn at com.opensymphony.xwork2.inject.util.ReferenceCache.get(ReferenceCache.java:116)rn……………………………………………………rn .但是将这个包删除了以后为什么还是报这个错呢??????后来我将web项目重新拷贝一份和原来一样的,运行就不报这个错了,这是为什么呢?请各位高手指教一下我这个菜鸟………………多谢多谢

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试