我试图使用 spring mvc + hibernate + postgresql 连接设置登录页面。经过几次点击和运行,下面的代码运行良好。我在设置时遇到了几个问题。来自@M 的评论。Deinum 帮助我完成了这项工作。他在回复我之前的问题时发布的一些有用材料也帮助我理解了 spring mvc + hibernate 概念。所以有问题的人可以参考下面的代码。只是分享给大家参考。
我的调度程序 servlet 文件是
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC @Controller programming model <mvc:annotation-driven /> -->
<annotation-driven/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/pages/"/>
<beans:property name="suffix" value=".jsp"/>
</beans:bean>
<beans:bean id="userValidator" class="com.springapp.mvc.validator.UserValidator"/>
<!--Data source has the database information -->
<beans:bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="${jdbc.driverClassName}"/>
<beans:property name="url" value="${jdbc.url}"/>
<beans:property name="username" value="${jdbc.username}"/>
<beans:property name="password" value="${jdbc.password}"/>
</beans:bean>
<!-- SessonFactory for Hibernate -->
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource"/>
<beans:property name="packagesToScan" value="com.springapp.mvc.domain"/>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</beans:prop>
<beans:prop key="hibernate.max_fetch_depth">3</beans:prop>
<beans:prop key="hibernate.jdbc.fetch_size">50</beans:prop>
<beans:prop key="hibernate.jdbc.batch_size">10</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<!-- Transaction Manager -->
<beans:bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory"/>
</beans:bean>
<context:property-placeholder location="/jdbc.properties"/>
<context:component-scan base-package="com.springapp.mvc"/>
<!-- bind your messages.properties -->
<beans:bean class="org.springframework.context.support.ResourceBundleMessageSource"
id="messageSource">
<beans:property name="basename" value="locale\messages"/>
<!-- <beans:property name="fallbackToSystemLocale" value="false" /> -->
</beans:bean>
</beans:beans>
我的 LoginDao 类文件是
package com.springapp.mvc.dao;
import com.springapp.mvc.domain.Users;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
@Transactional
@Repository("LoginDao")
public class LoginDao {
public SessionFactory getMy_sessionfactory() {
return my_sessionfactory;
}
@Resource(name = "sessionFactory")
public void setMy_sessionfactory(SessionFactory my_sessionfactory) {
this.my_sessionfactory = my_sessionfactory;
}
private SessionFactory my_sessionfactory;
public Long findByUname(Users loginForm) {
System.out.println("Hi Dao" + loginForm.getUname());
Session session = my_sessionfactory.getCurrentSession();
String hql = "select count(u.uname) from Users u where u.uname = ? and u.pass = ?";
Query query = session.createQuery(hql).setString(0, loginForm.getUname()).setString(1, loginForm.getPass());
System.out.println("query" + query);
System.out.println("query ur" + query.uniqueResult());
Long count = (Long) query.uniqueResult();
return count;
}
}
我的域类文件是
package com.springapp.mvc.domain;
import javax.persistence.*;
@Entity
@Table(name = "Users")
public class Users {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "uname")
private String uname;
@Column(name = "pass")
private String pass;
private Long user_count;
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public Long getUser_count() {
return user_count;
}
public void setUser_count(Long user_count) {
this.user_count = user_count;
}
}
我的控制器类文件是
package com.springapp.mvc.controller;
import com.springapp.mvc.domain.Users;
import com.springapp.mvc.service.LoginService;
import com.springapp.mvc.validator.UserValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/")
public class SigninController {
@Autowired
private LoginService lsp;
@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Hello world!");
return "appstore";
}
@RequestMapping(value = "/signin", method = RequestMethod.GET)
public String loginPage(ModelMap model) {
Users loginForm = new Users();
model.addAttribute("loginForm", loginForm);
return "signin";
}
@RequestMapping(value = "/loginsuccess", method = RequestMethod.GET)
public String loginSuccess(@ModelAttribute("loginForm") Users loginObj, ModelMap model, BindingResult result) {
Long count = this.lsp.validateLogin(loginObj);
loginObj.setUser_count(count);
UserValidator userValidator = new UserValidator();
userValidator.validate(loginObj, result);
model.addAttribute("message1", loginObj.getUname());
System.out.println("result error" + result.getAllErrors());
System.out.println("result error count" + result.getErrorCount());
if (result.hasErrors()) {
return "signin";
} else {
return "loginsuccess";
}
}
我的 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>D2DCOMM</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>D2DCOMM</name>
<properties>
<java-version>1.7</java-version>
<org.springframework-version>3.2.0.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>commons-dbcp-all</artifactId>
<version>1.3-r699049</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-sftp</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.1.4</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.10.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- JDBC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.2-1003-jdbc4</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.test.int1.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
我的验证器类
package com.springapp.mvc.validator;
import com.springapp.mvc.domain.Users;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
public class UserValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return Users.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object obj, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "uname", "Empty.UsersName");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "pass", "Empty.UsersPass");
Users user = (Users) obj;
System.out.println("count from validator!!!!!" + user.getUser_count());
if (user.getUser_count() > 0) {
} else {
errors.reject("User.Details.Mismatch");
}
}
}
我的 messages_en_US_properties 文件位于 (src/main/resources/locale/messages_en_US_properties) 文件如下
Empty.UsersName = User Name is required!
Empty.UsersPass = Password is required!
User.Details.Mismatch = No user details found.Please sign up.
我的 jdbc_properties 文件
jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost:5432/postgres
jdbc.username=postgr
jdbc.password=postgr