請幫助我解決使用DTO將值從DAO傳遞到服務層時遇到的問題。這是我的DAO課程。如何使用spring Hibernate將DTO的值(實體類的對象)設置爲通用列表<實體類>
@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> </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]
ProductDTO是一個簡單的POJO你爲什麼自動裝配它? 您應該使用ProductDaoImpl.getAllProduct()方法中的新運算符來實例化它。 – Ammar