问题描述
- 请问,juint测试service层可以delete,add,但是controller不行
-
谢谢各位大神指导!spring-hibernate.xml
<context:component-scan base-package="cn"></context:component-scan> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="root"></property> <property name="password" value="root"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sampledb"></property> <property name="initialPoolSize" value="3"></property> <property name="maxPoolSize" value="5"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hiberante.format_sql">true</prop> </props> </property> <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> --> <property name="mappingLocations" value="classpath:cn/bean/*.hbm.xml"></property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="find*" propagation="NEVER"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* cn.dao.impl.UserDaoHibernateImpl.*(..))" id="txPointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config>
spring-core.xml
<context:annotation-config /> <context:component-scan base-package="cn" />
spring-servlet.xml
<!-- 注解扫描的包 --> <context:component-scan base-package="cn" />
<mvc:annotation-driven /> <mvc:resources location="/img/" mapping="/img/**" /> <mvc:resources location="/js/" mapping="/js/**" /> <!-- 视图解释类 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 --> <property name="suffix" value=".jsp"></property> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJackson2HttpMessageConverter" /> </list> </property> </bean> <bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>text/json;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean>
web.xml
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</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> <!-- 控制Session的开关 --> <filter> <filter-name>openSession</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSession</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Controller
@Controller @RequestMapping({"/", "/user"}) public class UserCURDController { @Autowired UserService us; private static final Log logger = LogFactory.getLog(UserCURDController.class); @RequestMapping(value={"/","/show"}) public String getAllUsers(Model model) { List<User> users = us.findAllUser(); model.addAttribute("users", users); return "users/UserList"; } @RequestMapping(value={"/","/show"}, params="json") public @ResponseBody List<User> getAllUsers() { List<User> users = us.findAllUser(); return users; } @RequestMapping(value="/show/{id}", method=RequestMethod.GET) public String getUser(@PathVariable("id") Integer id, Model model) { model.addAttribute("user", us.findUserById(id)); return "users/UserDetail"; } @RequestMapping(value="/show/{id}", method=RequestMethod.GET, params="json") public @ResponseBody User getUser(@PathVariable("id") Integer id) { User u =us.findUserById(id); return u; } @RequestMapping(value="/edit/{id}", method=RequestMethod.GET) public String editUser(@PathVariable("id") Integer id, Model model) { User u = us.findUserById(id); model.addAttribute("user", u); return "users/UserEditForm"; } @RequestMapping(value = "/edit", method=RequestMethod.POST) public String editUser(User u, Model model) { System.out.println(u.getName()+" "+u.getPassword()); us.updateUser(u); return "redirect:/user/show"; } @RequestMapping(value="/add", method=RequestMethod.GET) public String addUser(Model model){ model.addAttribute("user", new User()); return "users/UserAddForm"; } @RequestMapping(value="/add", method=RequestMethod.POST) public String addUser(User u, BindingResult ret, HttpServletResponse response) throws BindException { if(ret.hasErrors()){ throw new BindException(); } us.addUser(u); return "redirect:/user/show"; } @RequestMapping(value="/delete/{id}", method=RequestMethod.GET) public String delete(@PathVariable("id") Integer id) { User u = us.findUserById(id); us.deleteUser(u); return "redirect:/user/show"; } }
解决方案
你service自己编写测试成功了是吧?
List users = us.findAllUser();在这里加人断点,看看us是否真的实例化了。同时可以验证是否真的拦截了
解决方案二:
在daoImpl中的类中,有:
private static final Log logger = LogFactory.getLog(UserDaoHibernateImpl.class);
相应方法中有:
logger.info("dao update");
控制台可以打印这条信息.
所以担心是事务没有配置好.
controller中的相应方法中也打印了浏览器发来的信息.
解决方案三:
service测试成功了,而control报错,应该就是对象创建实例化某个地方出问题了!
时间: 2024-09-09 17:49:25