• Spring MVC---基于注解的控制器


        基于注解的控制器

    • SpringMVC是一个基于DispatcherServlet的MVC框架,每个请求最先访问的是DispatcherServlet,DispatcherServlet负责将每一个Request转发到相应的Handler,Handler处理后再返回相应的模型(Model)和视图(View)。在使用注解的Spring MVC中,处理器Handler是基于@Controller和@RequestMapping两个注解的,这两个注解可以提供非常灵活的匹配和处理方式。
    @Controller和@RequestMapping注解
    • @Controller注解类型

      声明一个控制器类,Spring使用扫描机制来找到应用程序中所有基于注解的控制器类,控制器类的内部包含每个动作相应的处理方法,如下是一个@Controller的例子。

    复制代码
    package com.example.controller;
    
    import org.springframework.web.servlet.mvc.support.RedirectAttributes;
    ...
    @Controller
    public class ProductController {
        //request-handling methods here
    }
    复制代码

      为了保证Spring能扫描到控制器类,需要完成两个配置,首先要在Spring MVC的配置文件中声明Spring-context,如下所示:

    <beans 
        ...
        xmlns:context="http://www.springframework.org/schema/context"
        ...
    >

      其次需要应用<component-scan/>元素,在该元素中指定控制器类的基本包。例如所有的控制器类都在com.example.controller及其子包下,则该元素如下:

    <context:component-scan base-package="com.example.controller"/>

      确保所有控制器都在基本包下,并且不要指定太广泛的基本包,否则会导致Spring MVC扫描无关的包。

    • @RequestMapping注解类型

      该注解类型在控制器类的内部定义每一个动作相应的处理方法,一个采用@RequestMapping注释的方法将成为一个请求处理方法,并由调度程序在接收到对应的URL请求时调用,下面是一个RequestMapping注解方法的控制器类。

    复制代码
    package com.example.controller;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    ...
    
    @Controller
    public class ProductController {
        
        @RequestMapping(value="/productInput")
        public String inputProduct(){
            //do something here
            return "ProductForm";
        }
    }
    复制代码

      使用RequestMapping注解的value属性将URL映射到方法,在如上的例子中,我们将productInput映射到inputProduct()方法,通过http://localhost:8081/SpringMVC/productInput访问inputProduct方法。RequestMapping方法除了有value属性外,还有method属性,该属性用来指示该方法仅处理哪些http方法,例如,仅当HTTP POST或PUT方法时,调用下面的processOrder方法。

    @RequestMapping(value="/order_process", method={RequestMethod.POST, RequestMethod.PUT})
    public String processOrder(){
        //do something here
        return "OrderForm";
    }

      如果method属性只有一个HTTP方法值,则无需{},直接为method=RequestMethod.POST,如果未指定method属性,则请求处理方法可以处理任意HTTP方法。此外RequestMapping注释类型也可以用来注释一个控制器类,如下所示:

    复制代码
    import org.springframework.stereotype.Controller;
    ...
    
    @Controller
    @RequestMapping(value="/customer")
    public class CustomerController{
    @RequestMapping(value="/delete", method={RequestMethod.POST, RequestMethod.PUT})
    public String deleteCustomer(){
    //do something here
    return ...;
    } }
    复制代码

       在这种情况下,所有的方法都将映射为相对于类级别的请求,如例子中的deleteCustomer方法,由于控制器类映射使用"/customer",而deleteCustomer方法映射为"/delete",则需要通过http://localhost:8081/SpringMVC/customer/delete。

    应用基于注解的控制器

      本节将通过一个例子说明基于注解的控制器,展示了一个包含两个请求处理方法的控制器类,项目目录结构如下:

           

      我们首先对类进行介绍,Product是产品类,包括产品的名称、描述和价格属性,该类实现了Serializable接口,而ProductForm类与HTML表单相映射,是Product在服务端的代表,表单对象传递ServletRequest给其他组件,还有当数据校验失败时,表单对象将用于保存和展示用户在原始表单上的输入。DBOperator类负责与mysql数据库打交道,ProductService定义了服务层的接口,ProductServiceImpl实现了ProductService中定义的接口。

    Product类代码如下,包含属性和属性的set和get方法:

     View Code

    ProductForm代码如下,和Product类唯一区别就是不用实现Serializable接口:

     View Code

    DBOperator类包含两个方法,一个是执行sql语句,一个是从数据库中查询数据:

     View Code

      服务层的ProductService和实现类ProductServiceImpl如下,ProductServiceImpl类中通过@Autowired和@Service进行DBOperator对象的依赖,@Autowired注解会使DBOperator的一个实例被注入到ProductServiceImpl实例中,为了能是类能被spring扫描到,必须为其标注为@Service。

     View Code

      当然最重头戏的还是ProductController类,该类中的方法通过RequestMapping指定Request和Handler的对应关系,而在saveProduct方法的参数中通过RedirectAttribute实现页面的重定向。

     View Code

      如下是web.xml配置文件,应用servlet和servlet-mapping元素,servlet元素内的<load-on-startup>元素是可选的。如果存在,则它将在应用程序启动时装载servlet并调用它的init方法,若不存在,则在该servlet的第一个请求时加载,可以通过<init-param>中的<param-value>元素指定配置文件的路径,如果没有<init-param>元素,则默认路径为程序的WEB-INF目录下的servletName-servlet.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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <servlet>
          <servlet-name>springmvc</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>/WEB-INF/config/springmvc-config.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
          <servlet-name>springmvc</servlet-name>
          <url-pattern>/</url-pattern>
      </servlet-mapping>
    </web-app>
    复制代码

      如下是配置文件(springmvc-config.xml),其中包括指定控制器类所在的包、默认访问路径请求的html、配置数据源(注意用户名和密码需要改为自己数据库的用户名和密码)通过setter的方式在DBOperator对象中注入jdbcTemplate对象,最后通过viewResolver指定视图文件所在的路径以及后缀。

    复制代码
    <?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <context:component-scan base-package="com.example.controller"/> <context:component-scan base-package="service"/> <context:component-scan base-package="mysqloperator"/> <mvc:annotation-driven/>
    <mvc:resources mapping="/*.html" location="/"/> <!-- <bean name="/productInput" class="Controller.InputProductController"/> <bean name="/productSave" class="Controller.SaveProductController"/> --> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <!-- 数据库驱动 --> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <!-- 连接数据库的URL 数据库名为已经创建好的User --> <property name="url" value="jdbc:mysql://localhost/Product"/> <!-- 连接数据库的用户名 --> <property name="username" value="root"/> <!-- 连接数据的密码 --> <property name="password" value="123"/> </bean> <!-- 配置JDBC模板 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <!-- 默认必须使用数据源 --> <property name="dataSource" ref="dataSource"/> </bean> <bean id="dbOperator" class="mysqloperator.DBOperator"> <property name="jdbcTemplate" ref="jdbcTemplate"/> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp" /> </bean> </beans>
    复制代码

    商品表单视图(ProductForm.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>
    <div id="global">
    <form action="productSave" method="post">
        <fieldset>
            <legend>Add a product</legend>
            <p>
                <label for="name">Product Name:</label>
                <input type="text" id="name" name="name" tabindex="1">
            </p>
            <p>
                <label for="description">Description:</label>
                <input type="text" id="description" name="description" tabindex="2">
            </p>
            <p>
                <label for="price">Price:</label>
                <input type="text" id="price" name="price" tabindex="3">
            </p>
            <p id="buttons">
                <input id="reset" type="reset" tabindex="4">
                <input id="submit" type="submit" tabindex="5" value="Add Product">
            </p>
        </fieldset>
    </form>
    </div>
    </body>
    </html>
    复制代码

    商品详细信息表单(ProductDetail.jsp)问价如下:

    复制代码
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!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=ISO-8859-1">
    <title>Save Product</title>
    </head>
    <body>
    <div id="global">
        <h4>The product has been saved.</h4>
        <p>
            <h5>Details:</h5>
            Product Name: ${product.name }<br />
            Description: ${product.description } <br />
            Price: $${product.price }
        </p>
    </div>
    </body>
    </html>
    复制代码

      最后在Tomcat服务器下运行该web程序,报logging版本冲突异常(You have more than one version of 'org.apache.commons.logging.Log' visible,),将导入的jar包中的commons-logging-1.2.jar删除即可,并在浏览器地址栏中输入:http://localhost:8081/SpringMVC/productInput,即可实现对ProductForm.jsp视图的访问,然后输入商品信息,点击Add Product按钮,即可将商品信息添加到数据库(需要提前创建数据库(User)和数据库中的表(t_product(id,name,description,price))),并从数据库中查询插入信息并在ProductDetail.jsp视图中显示。

            

  • 相关阅读:
    验证SMTP工作过程
    FileZilla FTP服务器的安装和配置
    最后一块石头的重量
    不用加号的加法
    同构字符串
    最长公共子序列
    Telnet 验证HTTP工作过程
    矩阵的最小路径和
    子数组的最大累加和问题
    海思开发板——YOLOv3模型移植(4)
  • 原文地址:https://www.cnblogs.com/aeexiaoqiang/p/6528927.html
Copyright © 2020-2023  润新知