ssh框架搭建
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&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中启动服务
上一篇: 酒店机器人的前景怎么样?