SpringMVC入门第三部分

Posted 大忽悠爱忽悠

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringMVC入门第三部分相关的知识,希望对你有一定的参考价值。

利用SpringMVC做一个CRUD符合Rest风格的网站案例

Web环境搭建

1.导入相关坐标

  <!--导入spring的坐标-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
    <!--导入aspectj的坐标-->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.4</version>
    </dependency>
    <!--引入Spring测试坐标-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
    <!--junit坐标-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.1</version>
    </dependency>
    <!--mysql驱动的坐标-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.32</version>
    </dependency>
    <!--c3p0数据库连接池的坐标-->
    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>
    <!--druid数据库连接池坐标-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.10</version>
    </dependency>
    <!--spring jdbc的坐标-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
    <!--spring tx的坐标,处理事务的-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>

    <!--SpringWeb模块相关的坐标-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.5.RELEASE</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.0</version>
      <scope>provided</scope>
    </dependency>

    <!--jstl的依赖-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
<!--standard的依赖-->
    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>

2.web.xml进行配置

Tips: 字符编码的Filter配置要在支持REST风格的filter配置之前

<!--配置springmcv的前段控制器-->
  <servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--SpringMvc.xml是前段控制器需要使用-->
    <!--sevlet的初始化参数-->
    <!--contextConfigLocation:指定SpringMVC配置文件的位置-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:SpringMVC.xml</param-value>
    </init-param>
    <!--默认第一次访问时创建对象,这里填入1,表示服务器启动时创建镀锡-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <!--
    /和/*都是拦截所有请求,但是/*的范围更大,还会拦截到*.jsp这些请求
    一旦拦截jsp页面就不会显示了
    /会拦截所有请求,但是不会拦截*.jsp,能保证jsp访问正常
    -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!--配置一个字符编码的Filter-->
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <!--初始化参数-->
    <!--encoding:指定解决POST请求的乱码-->
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <!--forceEncoding:顺手解决响应乱码
    response.setCharacterEncoding(this.encoding)-->
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!--支持REST风格的filter-->
  <!--配置filter-->
  <filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <!--拦截所有请求-->
    <url-pattern>/*</url-pattern>
  </filter-mapping>

3.SpringMVC.xml配置

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

    <mvc:annotation-driven />
    <!--context的组件扫描,需要引入context命名空间-->
    <!--只需要扫描cotroller包下的内容-->
    <context:component-scan base-package="com"/>


    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
        <!--指定创建jstlView-->
        <!--  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>-->
    </bean>

</beans>

数据库环境搭建

在这里插入图片描述

1.propertise配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/tx
jdbc.username=root
jdbc.password=xxxxxxx

2.在Spring容器中引入pro配置文件,配置数据源,配置JDBCTemplate对象

SpringMVC.xml

    <!--加载jdbc.properties-->
    <!--引入context命名空间-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--数据源对象-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--jdbc模板对象-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

Dao层

EmployeeDao类:

@Repository
public class EmployeeDao {
@Autowired //按照类型注入
    JdbcTemplate jdbcTemplate;

    //保存员工信息到数据库
    public void save( Employee employee)
    {
        String  sql="insert employee values(?,?,?,?,?)";
          jdbcTemplate.update(sql,employee.getId(),employee.getName(),employee.getDepartment(),
                  employee.getEamil(),employee.getGender());
    }

    //查询并返回对应员工信息
    public Employee exist(Employee employee)
    {
        String  sql="select * from employee where name=? and id= ?";
try{
    Employee employee1 = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Employee>(Employee.class), employee.getName(), employee.getId());
      return employee1;
}
catch (Exception e)
{
return null;
}
    }

   //删除某个员工的信息
    public void delete(Employee employee)
    {
        String sql="delete from employee where id=?";
        jdbcTemplate.update(sql,employee.getId());
    }

    //返回所有员工的信息
    public List<Employee> getAllEmployees()
    {
        String sql="select* from employee";
        List<Employee> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Employee>(Employee.class));
    return list;
    }
}


POJO—自定义对象层

employee类:

@Component
public class Employee {
private Integer id;
private  String name;
private  String department;
private  String eamil;
private  Integer gender;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getEamil() {
        return eamil;
    }

    public void setEamil(String eamil) {
        this.eamil = eamil;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\\'' +
                ", department='" + department + '\\'' +
                ", eamil='" + eamil + '\\'' +
                ", gender=" + gender +
                '}';
    }
}


展示所有员工的界面设计思路

访问index.jsp---->直接发送/emps请求----->控制器查询所有员工-------->放在请求域中-------->转发到list页面进行展示

index.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%--访问页面就直接展示员工列表页面--%&g

以上是关于SpringMVC入门第三部分的主要内容,如果未能解决你的问题,请参考以下文章

python入门第三天

python入门第三十四天--事件驱动模型

python入门第三天

Spring入门第三十课

Java入门第二季学习总结

解析Mybatis入门第三天