Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- 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">
- <context:component-scan base-package="bellemaison.controller" />
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/jsp/" />
- <property name="suffix" value=".jsp" />
- </bean>
- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
- autowire="byName">
- <property name="driverClassName" value="org.postgresql.Driver" />
- <property name="url" value="jdbc:postgresql://localhost:5432/bellemaison" />
- <property name="username" value="user" />
- <property name="password" value="passw0rd" />
- </bean>
- <bean id="propertyDAO" class="bellemaison.dao.JdbcPropertyDAO">
- <property name="dataSource" ref="dataSource"></property>
- </bean>
- </beans>
- package bellemaison.model;
- public class Property {
- private int id;
- private String address;
- private int size;
- private int bedroom;
- private int bathroom;
- private double price;
- public Property() {
- }
- public Property(String address, int size, int bedroom, int bathroom, double price) {
- this.address = address;
- this.size = size;
- this.bedroom = bedroom;
- this.bathroom = bathroom;
- this.price = price;
- }
- public int getId() { return id; }
- public void setId(int id) { this.id = id; }
- public String getAddress() { return address; }
- public void setAddress(String address) { this.address = address; }
- public int getSize() { return size; }
- public void setSize(int size) { this.size = size; }
- public int getBedroom() { return bedroom; }
- public void setBedroom(int bedroom) { this.bedroom = bedroom; }
- public int getBathroom() { return bathroom; }
- public void setBathroom(int bathroom) { this.bathroom = bathroom; }
- public double getPrice() { return price; }
- public void setPrice(double price) { this.price = price; }
- }
- package bellemaison.dao;
- import bellemaison.model.Property;
- import java.util.List;
- public interface PropertyDAO {
- public void saveOrUpdate(Property property);
- public void delete(int propertyID);
- public Property get(int propertyID);
- public List<Property> list();
- }
- package bellemaison.dao;
- import bellemaison.model.Property;
- import org.springframework.dao.DataAccessException;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.jdbc.core.ResultSetExtractor;
- import org.springframework.jdbc.core.RowMapper;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.List;
- public class JdbcPropertyDAO implements PropertyDAO {
- private JdbcTemplate jdbcTemplate;
- private javax.sql.DataSource dataSource;
- public JdbcPropertyDAO(javax.sql.DataSource dataSource) {
- jdbcTemplate = new JdbcTemplate(dataSource);
- }
- public JdbcPropertyDAO() {
- }
- ...
- public List<Property> list() {
- String sql = "SELECT * FROM properties";
- List<Property> listProperty = jdbcTemplate.query(sql, new RowMapper<Property>() {
- @Override
- public Property mapRow(ResultSet resultSet, int i) throws SQLException {
- Property aProperty = new Property();
- aProperty.setId(resultSet.getInt("id"));
- aProperty.setAddress(resultSet.getString("address"));
- aProperty.setSize(resultSet.getInt("size"));
- aProperty.setBedroom(resultSet.getInt("bedroom"));
- aProperty.setBathroom(resultSet.getInt("bathroom"));
- aProperty.setPrice(resultSet.getDouble("price"));
- return aProperty;
- }
- });
- return listProperty;
- }
- public void setDataSource(javax.sql.DataSource dataSource) {
- this.dataSource = dataSource;
- }
- public javax.sql.DataSource getDataSource() {
- return dataSource;
- }
- };
- package bellemaison.controller;
- import bellemaison.dao.JdbcPropertyDAO;
- import bellemaison.dao.PropertyDAO;
- import bellemaison.model.Property;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.servlet.ModelAndView;
- import java.io.IOException;
- import java.util.List;
- @Controller
- public class Properties {
- @Autowired PropertyDAO propertyDAO;
- @RequestMapping("/properties_all")
- public ModelAndView listProperty(ModelAndView model) throws IOException {
- List<Property> listProperty = propertyDAO.list();
- model.addObject("listProperty", listProperty);
- model.setViewName("home");
- return model;
- }
- }
- <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <meta charset="utf-8">
- <link rel="stylesheet" href="main.css">
- <link href="/resources/css/main.css" rel="stylesheet">
- <title>Contact Result</title>
- </head>
- <body>
- <div align="center">
- <h1>Property List</h1>
- <table border="1">
- <th>Address</th>
- <th>Size</th>
- <th>Bedroom</th>
- <th>Bathroom</th>
- <th>Price</th>
- <c:forEach var="property" items="${listProperty}" varStatus="status">
- <tr>
- <td>${property.address}</td>
- <td>${property.size}</td>
- <td>${property.bedroom}</td>
- <td>${property.bathroom}</td>
- <td>${property.price}</td>
- </tr>
- </c:forEach>
- </table>
- </div>
- </body>
- </html>
- HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException
- type Exception report
- message Request processing failed; nested exception is java.lang.NullPointerException
- description The server encountered an internal error that prevented it from fulfilling this request.
- exception
- org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
- org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
- org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
- javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
- org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
- javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
- org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
- root cause
- java.lang.NullPointerException
- bellemaison.dao.JdbcPropertyDAO.list(JdbcPropertyDAO.java:80)
- bellemaison.controller.Properties.listProperty(Properties.java:23)
- sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
- sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
- java.lang.reflect.Method.invoke(Method.java:497)
- org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180)
- org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
- org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
- org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
- org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
- org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
- org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
- javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
- org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
- javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
- org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement