1. 程式人生 > >ssh框架搭建

ssh框架搭建

eas _for window exceptio url tomcat tle -m rec

1.搭建環境windows10+eclipse4.8+jdk1.8+maven

2.框架springMVC+spring+Hibernate

3.搭建步驟

1)首先創建maven項目

file--new--other---maven-project

2)配置pom文件

spring-core spring-context spring-web spring-webmvc spring-orm hibernate-core hibernate-entitymanager mysql-connector-java

<?xml version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>cn.temptation</groupId>
<artifactId>ssh</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>ssh Maven Webapp</name>
<!-- FIXME change it to the project‘s website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.version>4.2.6.RELEASE</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>

<!-- springframework 4 dependencies begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</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>
</dependency>
<!-- springframework 4 dependencies end -->

<!-- hibernate 配置 begin -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.9.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.9.Final</version>
</dependency>
<!-- hibernate 配置 end -->

<!-- mysql數據庫的驅動包 start -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>

<!-- 引入jstl包 -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>

<!-- 引入servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>

<!-- 引入數據庫連接池 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
</dependencies>

<build>
<finalName>ssh</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

3)創建對應的資源包 src/main/java src/main/resources

4)添加配置文件

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd">

<!-- 對web包中的所有類進行掃描,以完成Bean創建和自動依賴註入的功能 -->
<context:component-scan base-package="cn.temptation" />

<!-- 激活基於註解的配置 @RequestMapping, @ExceptionHandler,數據綁定 ,@NumberFormat ,
@DateTimeFormat ,@Controller ,@Valid ,@RequestBody ,@ResponseBody等 -->
<mvc:annotation-driven />

<!-- 視圖層配置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--配置JSTL表達式-->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!-- 前綴 -->
<property name="prefix" value="/WEB-INF/view/"/>
<!-- 後綴 -->
<property name="suffix" value=".jsp"/>
</bean>

<!-- 配置靜態資源 -->
<mvc:resources location="/res/" mapping="/res/**"/>
</beans>

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName"
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<!-- 對web包中的所有類進行掃描,以完成Bean創建和自動依賴註入的功能 -->
<context:component-scan base-package="cn.temptation" />

<!-- 數據源,BasicDataSource,commons-dbcp -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/ket-test?useUnicode=true&amp;characterEncoding=UTF-8" />
<property name="username" value="root" />
</bean>

<!-- Hibernate SesssionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingLocations" value="classpath:/cn/temptation/entity/*.hbm.xml" />
</bean>

<!-- PlatTranscationManager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />
</beans>

4)修改web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">

<display-name>Archetype Created Web Application</display-name>

<!-- 配置spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>

<!-- 配置監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 請求都交給DispatcherServlet處理 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!-- 增加中文亂碼過濾器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 清除jsp空格 -->
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
</jsp-property-group>
</jsp-config>

<!--<welcome-file-list>
<welcome-file>/view/login.jsp</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>-->

</web-app>

5)編寫測試的控制層和邏輯層代碼

LoginController

package cn.temptation.controller;

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 cn.temptation.entity.User;
import cn.temptation.service.LoginService;

/**
* @version
* @time 2018-2-28 下午5:57:07
* @describe:控制器
*/
@Controller
// 註解為控制器
@RequestMapping(value = "/login")
// 截獲帶有/login的請求
public class LoginController {
@Autowired
LoginService loginService; // 註入service層

@RequestMapping(method = RequestMethod.GET)
public String get() { // 用來返回一個頁面
return "login"; // 返回指向login.jsp頁面
}

@RequestMapping(method = RequestMethod.POST)
public String post(User user) { // 用來處理用戶的登陸請求
if (loginService.login(user.getUserName(), user.getPassword()) == 1) {
return "login_success"; // 登陸成功,跳轉到login_success.jsp頁面
}
return "login";

}
}

LoginService

package cn.temptation.service;

public interface LoginService {
public int login(String userName, String password);
}

LoginServiceImpl

package cn.temptation.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.temptation.dao.UserDao;

@Service
public class LoginServiceImpl implements LoginService {
@Autowired
private UserDao userDao;

@Override
public int login(String userName, String password) {
return userDao.find(userName, password) == null ? 0 : 1;
}

}

UserDao

package cn.temptation.dao;

import cn.temptation.entity.User;

public interface UserDao {
public User find(String userName, String password);
}

UserDaoImpl

package cn.temptation.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import cn.temptation.entity.User;

@Repository
public class UserDaompl extends HibernateDaoSupport implements UserDao {

// 用來註入sessionFactory(不註入會報錯)
@Resource(name = "sessionFactory")
public void setSessionFactoryOverride(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}

@Override
public User find(String userName, String password) {
List<?> users = getHibernateTemplate()
.find("from User where userName=? and password=?", userName,
password);
return users.size() > 0 ? (User) users.get(0) : null;// 通過Hibernate去數據庫取
}

}

User

package cn.temptation.entity;

public class User {
private Integer id;
private String userName;
private String password;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", password="
+ password + "]";
}

}

User.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="cn.temptation.entity">
<class name="User" table="user">
<id name="id">
<generator class="native"/>
</id>
<property name="userName" length="12" not-null="true" unique="true"/>
<property name="password" length="12" not-null="true"/>
</class>
</hibernate-mapping>

6)編寫視圖層的jsp代碼

在webapp下創建新的文件夾view將測試的登錄和登錄成功的jsp放到這個文件夾下

login.jsp

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="cn.temptation.entity">
<class name="User" table="user">
<id name="id">
<generator class="native"/>
</id>
<property name="userName" length="12" not-null="true" unique="true"/>
<property name="password" length="12" not-null="true"/>
</class>
</hibernate-mapping>

login-success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陸成功</title>
</head>
<body>
登陸成功
</body>
</html>

7)項目部署到tomcat中啟動服務

ssh框架搭建