0

请帮助我解决我在使用 DTO 将值从 DAO 传递到服务层时遇到的这个问题。这是我的 DAO 课程。

@Repository
public class ProductDaoImpl implements ProductDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    private ProductDTO productDTO;
    //ProductDTO productDTO=new ProductDTO();

        @SuppressWarnings("unchecked")
        @Override
        public ProductDTO getAllProduct(){
        //public  List<Product_Catalog> getAllProduct(){ 
             //return this.sessionFactory.getCurrentSession().getNamedQuery("findAllProduct").list();


            Session session = sessionFactory.getCurrentSession();
                Query query=(Query) session.getNamedQuery("findAllProduct");
               // query.setInteger("productCatalogId", id);
               List<Product_Catalog> product=query.list();

               for (Iterator iterator = product.iterator(); iterator.hasNext();) {
                   Product_Catalog products = (Product_Catalog) iterator.next();
                   productDTO.setMaxPrice(products.getMaxPrice());
                   productDTO.setMinPrice(products.getMinPrice());
                   System.out.println("Max Price " + products.getMaxPrice());
                   System.out.println("Max Price " + productDTO.getMaxPrice());

               }

            return productDTO;
}
}

DTO类:

 public class ProductDTO implements java.io.Serializable {

         private int pincode;
         private int productCatalogId ;
         private Requirement_type requirementTypeId ;
         private Boolean isActive ;
         private Boolean isRequesterProduct ;
         private float maxPrice ;
         private float minPrice;
//getter ,setter 
}

ServiceImpl 类:-

 @Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDAO productDAO;

    List<Product_Catalog> product=new ArrayList<Product_Catalog>();

    @SuppressWarnings("unchecked")
    @Override
    @Transactional
    public  List<Product_Catalog> getAllProduct(){ 

        ProductDTO productDTO= productDAO.getAllProduct();

            /*product.addAll(productDTO.getMaxPrice());
            product.addAll(0, Collection<Product_Catalog>c);
            product.add(productDTO.getMinPrice());*/



        // return productDAO.getAllProduct();
        return product;
    }
}

控制器类:-

@Controller
public class ProductController {

    @Autowired
    private ProductService productService;

     @RequestMapping(value = "/", method = RequestMethod.GET)
        public String listProducts(ModelMap map)
        {
            map.addAttribute("product", new Product_Catalog());
            map.addAttribute("productList", productService.getAllProduct());

            return "productList";
        }
}

实体类:-

@NamedQueries({
    @NamedQuery(
    name = "findAllProduct",
   query = "from Product_Catalog"

    )
})

@Entity
public class Product_Catalog extends baseEntity.Entity {
    @Id
    @Column(name="Product_Catalog_Id")
    private int productCatalogId ;


}

jsp:-

<html>
<head>
    <title>Spring 3 Hibernate integration </title>
</head>
<body>

<h2>Product List</h2>


<h3>Products</h3>
<c:if  test="${!empty productList}">
<table class="data">
<tr>
    <!-- <th>Product_Catalog_Id</th> -->
    <!-- <th>Requirement_Type_Id</th> -->
    <th>Max Price</th>
    <th>Min_Price</th>
    <th>&nbsp;</th>
</tr>
<c:forEach items="${productList}" var="product">
    <tr>

        <%-- <td>${product.productCatalogId}</td> --%>
        <%-- <td>${product.requirementTypeId}</td> --%>
        <td>${product.maxPrice}</td>
        <td>${product.minPrice}</td>
       <%--  <td><a href="delete/${prd.id}">delete</a></td> --%>
    </tr>
</c:forEach>
</table>
</c:if>

</body>
</html>

我的代码在不使用 DTO 的情况下运行良好,但是在从服务类中的 DTO 获取值时遇到问题。请建议我如何设置从 dto 到列表的值以在 jsp 页面上显示列表。下面的堆栈跟踪:-

15:05:44,073 INFO  [stdout] (http--127.0.0.1-8080-1) Max Price 20000.0

15:05:44,073 INFO  [stdout] (http--127.0.0.1-8080-1) Max Price 20000.0

15:05:44,073 INFO  [stdout] (http--127.0.0.1-8080-1) Max Price 50000.0

15:05:44,073 INFO  [stdout] (http--127.0.0.1-8080-1) Max Price 50000.0

15:05:44,619 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/usermanagement].[jsp]] (http--127.0.0.1-8080-1) Servlet.service() for servlet jsp threw exception: javax.el.PropertyNotFoundException: The class 'java.lang.Float' does not have the property 'maxPrice'.
    at javax.el.BeanELResolver.getBeanProperty(BeanELResolver.java:661) [jboss-el-api_2.2_spec-1.0.0.Final.jar:1.0.0.Final]
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:290) [jboss-el-api_2.2_spec-1.0.0.Final.jar:1.0.0.Final]
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175) [jboss-el-api_2.2_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.el.parser.AstValue.getValue(AstValue.java:134) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:187) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:935) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.jsp.WEB_002dINF.views.productList_jsp._jspx_meth_c_005fforEach_005f0(productList_jsp.java:158)
    at org.apache.jsp.WEB_002dINF.views.productList_jsp._jspx_meth_c_005fif_005f0(productList_jsp.java:116)
    at org.apache.jsp.WEB_002dINF.views.productList_jsp._jspService(productList_jsp.java:76)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:326) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:253) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:734) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:541) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:479) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:407) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238) [spring-webmvc-3.2.2.RELEASE.jar:]
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264) [spring-webmvc-3.2.2.RELEASE.jar:]
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208) [spring-webmvc-3.2.2.RELEASE.jar:]
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992) [spring-webmvc-3.2.2.RELEASE.jar:]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939) [spring-webmvc-3.2.2.RELEASE.jar:]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) [spring-webmvc-3.2.2.RELEASE.jar:]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936) [spring-webmvc-3.2.2.RELEASE.jar:]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827) [spring-webmvc-3.2.2.RELEASE.jar:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) [spring-webmvc-3.2.2.RELEASE.jar:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:139) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]
    at org.jboss.as.web.NamingValve.invoke(NamingValve.java:57) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:154) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:667) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:952) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at java.lang.Thread.run(Thread.java:662) [:1.6.0_43]
4

2 回答 2

0

您的一段代码@Autowired private ProductDAO productDAO;在 Web 应用程序中是一场灾难,您如何保证这样一个自动装配字段的线程安全。正确的方法是使用 new 运算符实例化 ProductDTO 并简单地返回它。

错误的原因可能是您的 Product_Catalog 类似乎没有设置器和获取器。

于 2013-08-02T11:30:26.423 回答
0

您的代码失败的原因在这里:

 product.addAll(0, Collection<Product_Catalog>c);
 product.add(productDTO.getMinPrice());

您评论了此代码,但您要做的是在Float产品列表中插入一个。JSPFloat从列表中获取并尝试调用Float.getMinPrice().

为避免这种情况,请将产品放入List.

于 2013-08-02T14:28:03.013 回答