但是controller不行"

问题描述

请问,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

但是controller不行&quot;的相关文章

mvc4-MVC4 中Html.beginForm(&amp;amp;quot;actionName&amp;amp;quot;,&amp;amp;quot;controller&amp;amp;quot;,Formether.post)

问题描述 MVC4 中Html.beginForm("actionName","controller",Formether.post) 为什么我向控制器里的方法提交,提交过去之后,为什么报错,总提示找到视图! 解决方案 我记得是这样的吧:Html.BeginForm("actionName", "controller", FormMethod.Post)

服务器-URL传参中文乱码,new string(&amp;amp;quot;iso-8859-1&amp;amp;quot;,&amp;amp;quot;utf-8&amp;amp;quot;)方法不行

问题描述 URL传参中文乱码,new string("iso-8859-1","utf-8")方法不行 小白接触后台不多,用myecplise开了一个服务器(tomcat),其中有url参数:http://xxx(ip)/downloadservlet?name=小儿鼻出血.mp4 ,然后出现乱码,服务器无法定位到该文件. 查网发现大部分对name进行转码,用name= new string("iso-8859-1","utf-8&qu

Session(&amp;amp;quot;S_KUBUN&amp;amp;quot;,String)好像不行 可以给session一个类型麽?

问题描述 Session("S_KUBUN",String)好像不行可以给session一个类型麽? 解决方案 解决方案二:C#//赋值Session["S_KUBUN"]="xxxx";//取值stringaa=Session["S_KUBUN"].ToString();解决方案三:好像不行,Session的值都是Object类型的解决方案四:没必要呀解决方案五:session是HttpSessionState的一个对象解决

泸州属南气矿----配置S2600磁盘阵列报错:There is no master controller.

问题现象:   配置S2600磁盘阵列报错:There is no master controller.   Please input the array controller A IP address: 192.168.8.39 Please input the array controller B IP address: 192.168.8.40   ***** Error: There is no master controller.    = OK: install expectpkg 

spring mvc-springmvc controller Mapping不能和方法Mapping一样?

问题描述 springmvc controller Mapping不能和方法Mapping一样? 方法名一样就404了,不一样就可以找到,为什么?如/goods/goods就不行,改为/goods/getGoods就可以.

asp net mvc 跨控制器-asp.net mvc 怎么实现跨 controller 传参数?

问题描述 asp.net mvc 怎么实现跨 controller 传参数? 先来描述一下我的问题吧,asp.net mvc 在一个控制器的action中,有一个 list集合,要传到 另一个controller的action中,我知道的:1如果是同一个controller 可以 用this.(action名)来调用.因为是不同的controller所以不行.2还可以用RedirectToAction 输入控制器名,action名,然后是要传的参数,这样做,服务器会发一个302到浏览器,然后浏览

springmvc-spring使用JUnit时autowired报错,controller里面autowired可以的

问题描述 spring使用JUnit时autowired报错,controller里面autowired可以的 试了好多办法还是不行,不知道哪里配错了..麻烦大神帮看一下 异常信息: SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5f98de7] to p

ajax spring-ajax方式提交表单,spring mvc的Controller中怎么不能返回视图

问题描述 ajax方式提交表单,spring mvc的Controller中怎么不能返回视图 ajax请求到springmvc的Controller后,请求成功后为什么不跳转到jsp?就是这句:return new ModelAndView(""/sys/selectAccount"");不用ajax请求就可以 解决方案 使用ajax提交的请求 控制器中的重定向 或者请求转发都是无效的 ajax要跳转页面 只能把你要跳转的页面发送到前端 前端ajax的回调函数使用前

spring mvc注解不能访问到controller

问题描述 spring mvc注解不能访问到controller web.xml配置 <servlet> <servlet-name>annomvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLoca