У меня есть вэб приложение c использованием Spring MVC, которое берет данные с базы(postgresql) и выводит в HTML формате.
Выводит ошибки:
1)HTTP Status 500 - Servlet.init() for servlet dispatcher threw exception(The server encountered an internal error that prevented it from fulfilling this request.)
После покапался в логах, нашел еще ошибку
2) java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432/postgres
P.S: Я специально не использую JdbcTemplate
1) ContactDAOpublic interface ContactDAO {
public ArrayList<Contact> getContacts();
}
2)ContactDAOImplpublic class ContactDAOImpl implements ContactDAO {
private static Connection con = null;
public ContactDAOImpl(String dbUrl, String user, String pass) throws SQLException {
con = DriverManager.getConnection(dbUrl, user, pass);
}
@Override
public ArrayList<Contact> getContacts() {
ArrayList<Contact> list = null;
try {
list = new ArrayList<Contact>();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM contact");
while (rs.next()) {
Contact aContact = new Contact();
aContact.setId(rs.getInt("contact_id"));
aContact.setName(rs.getString("name"));
aContact.setEmail(rs.getString("email"));
aContact.setAddress(rs.getString("address"));
aContact.setTelephone(rs.getString("telephone"));
list.add(aContact);
}
rs.close();
st.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return list;
}
}
3)HomeController@Controller
public class HomeController {
@Autowired
private ContactDAO contactDAO;
@RequestMapping(value="/")
public ModelAndView listContact(ModelAndView model) throws IOException{
ArrayList<Contact> contacts = contactDAO.getContacts();
model.addObject("listContact", contacts);
model.setViewName("home");
return model;
}
}
4)Java Config@Configuration
@ComponentScan(basePackages="com.springapp.mvc")
@EnableWebMvc
public class MVCConfiguration extends WebMvcConfigurerAdapter{
private static String dbURL = "jdbc:postgresql://localhost:5432/postgres";
private static String username = "postgres";
private static String password = "borntorun";
@Bean
public ViewResolver getViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Bean
public void regDriver() {
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Bean
public ContactDAO getContactDAO() throws SQLException {
return new ContactDAOImpl(dbURL, username, password);
}
}
5)web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>SpringMVC</display-name>
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.springapp.mvc</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
6)pom.xml<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springapp</groupId>
<artifactId>SpringMVC</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringMVC</name>
<properties>
<spring.version>4.1.4.RELEASE</spring.version>
<cglib.version>2.2.2</cglib.version>
</properties>
<dependencies>
<!-- Spring core & mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<!-- CGLib for @Configuration -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>${cglib.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Servlet Spec -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>SpringMVC</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>