• 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&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中启动服务

  • 相关阅读:
    web设计_6_图片/标题/说明文字布局
    web设计_5_自由的框式组件
    web设计_4_可扩展的行
    ubuntu下把python脚本转为二进制字节码文件
    Ubuntu与Window双系统安装的注意事项
    安装Windows和Ubuntu双系统2
    安装Windows和Ubuntu双系统
    Configuring and Running Django + Celery in Docker Containers
    ggplot绘图之基本语法
    R Multiple Plots
  • 原文地址:https://www.cnblogs.com/wangjiec/p/9717415.html
Copyright © 2020-2023  润新知