Advertisement
Guest User

Untitled

a guest
Sep 1st, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.59 KB | None | 0 0
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6. http://www.springframework.org/schema/context
  7. http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  8.  
  9. <context:component-scan base-package="bellemaison.controller" />
  10.  
  11. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  12. <property name="prefix" value="/jsp/" />
  13. <property name="suffix" value=".jsp" />
  14. </bean>
  15.  
  16. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
  17. autowire="byName">
  18. <property name="driverClassName" value="org.postgresql.Driver" />
  19. <property name="url" value="jdbc:postgresql://localhost:5432/bellemaison" />
  20. <property name="username" value="user" />
  21. <property name="password" value="passw0rd" />
  22. </bean>
  23.  
  24. <bean id="propertyDAO" class="bellemaison.dao.JdbcPropertyDAO">
  25. <property name="dataSource" ref="dataSource"></property>
  26. </bean>
  27.  
  28. </beans>
  29.  
  30. package bellemaison.model;
  31.  
  32. public class Property {
  33. private int id;
  34. private String address;
  35. private int size;
  36. private int bedroom;
  37. private int bathroom;
  38. private double price;
  39.  
  40. public Property() {
  41.  
  42. }
  43.  
  44. public Property(String address, int size, int bedroom, int bathroom, double price) {
  45. this.address = address;
  46. this.size = size;
  47. this.bedroom = bedroom;
  48. this.bathroom = bathroom;
  49. this.price = price;
  50. }
  51.  
  52. public int getId() { return id; }
  53.  
  54. public void setId(int id) { this.id = id; }
  55.  
  56. public String getAddress() { return address; }
  57.  
  58. public void setAddress(String address) { this.address = address; }
  59.  
  60. public int getSize() { return size; }
  61.  
  62. public void setSize(int size) { this.size = size; }
  63.  
  64. public int getBedroom() { return bedroom; }
  65.  
  66. public void setBedroom(int bedroom) { this.bedroom = bedroom; }
  67.  
  68. public int getBathroom() { return bathroom; }
  69.  
  70. public void setBathroom(int bathroom) { this.bathroom = bathroom; }
  71.  
  72. public double getPrice() { return price; }
  73.  
  74. public void setPrice(double price) { this.price = price; }
  75.  
  76. }
  77.  
  78. package bellemaison.dao;
  79.  
  80. import bellemaison.model.Property;
  81.  
  82. import java.util.List;
  83.  
  84.  
  85. public interface PropertyDAO {
  86.  
  87. public void saveOrUpdate(Property property);
  88.  
  89. public void delete(int propertyID);
  90.  
  91. public Property get(int propertyID);
  92.  
  93. public List<Property> list();
  94. }
  95.  
  96. package bellemaison.dao;
  97.  
  98. import bellemaison.model.Property;
  99. import org.springframework.dao.DataAccessException;
  100. import org.springframework.jdbc.core.JdbcTemplate;
  101. import org.springframework.jdbc.core.ResultSetExtractor;
  102. import org.springframework.jdbc.core.RowMapper;
  103.  
  104. import java.sql.ResultSet;
  105. import java.sql.SQLException;
  106. import java.util.List;
  107.  
  108.  
  109. public class JdbcPropertyDAO implements PropertyDAO {
  110. private JdbcTemplate jdbcTemplate;
  111. private javax.sql.DataSource dataSource;
  112.  
  113. public JdbcPropertyDAO(javax.sql.DataSource dataSource) {
  114. jdbcTemplate = new JdbcTemplate(dataSource);
  115. }
  116.  
  117. public JdbcPropertyDAO() {
  118. }
  119.  
  120. ...
  121.  
  122. public List<Property> list() {
  123. String sql = "SELECT * FROM properties";
  124. List<Property> listProperty = jdbcTemplate.query(sql, new RowMapper<Property>() {
  125.  
  126. @Override
  127. public Property mapRow(ResultSet resultSet, int i) throws SQLException {
  128. Property aProperty = new Property();
  129.  
  130. aProperty.setId(resultSet.getInt("id"));
  131. aProperty.setAddress(resultSet.getString("address"));
  132. aProperty.setSize(resultSet.getInt("size"));
  133. aProperty.setBedroom(resultSet.getInt("bedroom"));
  134. aProperty.setBathroom(resultSet.getInt("bathroom"));
  135. aProperty.setPrice(resultSet.getDouble("price"));
  136.  
  137. return aProperty;
  138. }
  139. });
  140.  
  141. return listProperty;
  142.  
  143. }
  144.  
  145. public void setDataSource(javax.sql.DataSource dataSource) {
  146. this.dataSource = dataSource;
  147. }
  148.  
  149. public javax.sql.DataSource getDataSource() {
  150. return dataSource;
  151. }
  152.  
  153. };
  154.  
  155. package bellemaison.controller;
  156.  
  157. import bellemaison.dao.JdbcPropertyDAO;
  158. import bellemaison.dao.PropertyDAO;
  159. import bellemaison.model.Property;
  160. import org.springframework.beans.factory.annotation.Autowired;
  161. import org.springframework.stereotype.Controller;
  162. import org.springframework.web.bind.annotation.RequestMapping;
  163. import org.springframework.web.bind.annotation.RequestMethod;
  164. import org.springframework.web.servlet.ModelAndView;
  165.  
  166. import java.io.IOException;
  167. import java.util.List;
  168.  
  169.  
  170. @Controller
  171. public class Properties {
  172.  
  173. @Autowired PropertyDAO propertyDAO;
  174.  
  175. @RequestMapping("/properties_all")
  176. public ModelAndView listProperty(ModelAndView model) throws IOException {
  177. List<Property> listProperty = propertyDAO.list();
  178. model.addObject("listProperty", listProperty);
  179. model.setViewName("home");
  180.  
  181. return model;
  182. }
  183.  
  184. }
  185.  
  186. <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
  187. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  188. <html>
  189. <head>
  190. <meta charset="utf-8">
  191. <link rel="stylesheet" href="main.css">
  192. <link href="/resources/css/main.css" rel="stylesheet">
  193. <title>Contact Result</title>
  194. </head>
  195. <body>
  196. <div align="center">
  197. <h1>Property List</h1>
  198. <table border="1">
  199. <th>Address</th>
  200. <th>Size</th>
  201. <th>Bedroom</th>
  202. <th>Bathroom</th>
  203. <th>Price</th>
  204.  
  205. <c:forEach var="property" items="${listProperty}" varStatus="status">
  206. <tr>
  207. <td>${property.address}</td>
  208. <td>${property.size}</td>
  209. <td>${property.bedroom}</td>
  210. <td>${property.bathroom}</td>
  211. <td>${property.price}</td>
  212. </tr>
  213. </c:forEach>
  214. </table>
  215. </div>
  216. </body>
  217. </html>
  218.  
  219. HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException
  220.  
  221. type Exception report
  222.  
  223. message Request processing failed; nested exception is java.lang.NullPointerException
  224.  
  225. description The server encountered an internal error that prevented it from fulfilling this request.
  226.  
  227. exception
  228.  
  229. org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
  230. org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
  231. org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
  232. javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
  233. org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
  234. javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
  235. org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  236. root cause
  237.  
  238. java.lang.NullPointerException
  239. bellemaison.dao.JdbcPropertyDAO.list(JdbcPropertyDAO.java:80)
  240. bellemaison.controller.Properties.listProperty(Properties.java:23)
  241. sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  242. sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  243. sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  244. java.lang.reflect.Method.invoke(Method.java:497)
  245. org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180)
  246. org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
  247. org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
  248. org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
  249. org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
  250. org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
  251. org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
  252. javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
  253. org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
  254. javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
  255. org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement