SpringMVC

作者: ZOnePerson | 来源:发表于2018-08-10 16:03 被阅读0次

https://www.cnblogs.com/MrSaver/p/6424291.html

https://blog.csdn.net/hon_3y/article/details/71422914

SpringMVC

  1. SpringMVC介绍
  2. 入门程序
    --1.导包
    --2.创建springmvc.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-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!-- 配置controller扫描包 -->
    <context:component-scan base-package="cn.itcast.springmvc.controller" />

</beans>

--3.配置约束
--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"
    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">
    <display-name>springmvc-first</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

    <!-- 配置SpringMVC前端控制器 -->
    <servlet>
        <servlet-name>springmvc-first</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定SpringMVC配置文件 -->
        <!-- SpringMVC的配置文件的默认路径是/WEB-INF/${servlet-name}-servlet.xml -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc-first</servlet-name>
        <!-- 设置所有以action结尾的请求进入SpringMVC -->
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
</web-app>

--5.加入jsp页面
itemList,jsp

--6.创建pojo

public class Item {
    // 商品id
    private int id;
    // 商品名称
    private String name;
    // 商品价格
    private double price;
    // 商品创建时间
    private Date createtime;
    // 商品描述
    private String detail;

//创建带参数的构造器
set/get。。。
}

--7.创建controller

    public class ItemController{
        @ReequestMapping("/itemList.action")
        public MOodelAndView queryList(){
            List<Item> list = new ArrayList<>();
        list.add(new Item(1, "1华为 荣耀8", 2399, new Date(), "质量好!1"));
        list.add(new Item(2, "2华为 荣耀8", 2399, new Date(), "质量好!2"));
        list.add(new Item(3, "3华为 荣耀8", 2399, new Date(), "质量好!3"));
        list.add(new Item(4, "4华为 荣耀8", 2399, new Date(), "质量好!4"));
        list.add(new Item(5, "5华为 荣耀8", 2399, new Date(), "质量好!5"));
        list.add(new Item(6, "6华为 荣耀8", 2399, new Date(), "质量好!6"));

        //
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("list",list);

        return modelAndView;

        }
    }

--8.启动项目测试

试图解析器:
前缀+逻辑视图名+后缀

修改controller

modelAndView.setView("itemList");

return modelAndView;
测试效果:

  1. SpringMVC架构讲解

     1. 框架结构
     2. 组件说明
    
  2. SpringMVC整合MyBatis
    控制层:springmvc
    持久层:mybatis

    创建表:
    jar包:

加入jsp页面

  1. 参数绑定

     1. SpringMVC默认支持的类型
     2. 简单数据类型
     3. Pojo类型
     4. Pojo包装类型
     5. 自定义参数绑定
    
  2. SpringMVC和Struts2的区别


1. 高级参数绑定

    1. 数组类型的参数绑定
    2. List类型的绑定
2. @RequestMapping注解的使用
3. Controller方法返回值
4. SpringMVC中异常处理
5. 图片上传处理
6. Json数据交互
7. SpringMVC实现RESTful

8、拦截器

相关文章

网友评论

      本文标题:SpringMVC

      本文链接:https://www.haomeiwen.com/subject/njrvcftx.html