码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC环境搭建

时间:2018-06-15 01:08:37      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:source   imp   cpp   udf   beans   padding   otf   pve   dad   

1、新建WEB项目,导入springmvc的jar包

技术分享图片

 

2、在web.xml中配置DispatcherServlet

  <!-- servlet前端控制器DispatcherServlet --> 
  <servlet>
      <servlet-name>springMvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <!-- 如果没有指定springMvc核心配置文件,那么就会默认去找/WEB-INF/<servlet-name>中的内容+ -servlet.xml配置文件 -->
      <!-- 执行springmvc核心配置文件 -->
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:SpringMvc.xml</param-value>
      </init-param>
      <!-- 在Tomcat启动时候就加载这个servlet -->
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>springMvc</servlet-name>
      <url-pattern>*.action</url-pattern>
  </servlet-mapping>

 

3、实体类和控制类编写

实体类:Items.java

package cn.itheima.pojo;

import java.util.Date;

public class Items {
    
    private Integer id;
    private String name;
    private Float price;
    private String pic;
    private Date createtime;
    private String detail;

    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 == null ? null : name.trim();
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public String getPic() {
        return pic;
    }

    public void setPic(String pic) {
        this.pic = pic == null ? null : pic.trim();
    }

    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail == null ? null : detail.trim();
    }
}

控制类ItemsController.java

package cn.itheima.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.itheima.pojo.Items;

@Controller
public class ItemsController {
    
    @RequestMapping("/list")
    public ModelAndView itemsList() throws Exception {
        List<Items>itemList = new ArrayList<>();
        
        //商品列表
        Items items_1 = new Items();
        items_1.setName("联想笔记本_3");
        items_1.setPrice(6000f);
        items_1.setDetail("ThinkPad T430 联想笔记本电脑!");
        
        Items items_2 = new Items();
        items_2.setName("苹果手机");
        items_2.setPrice(5000f);
        items_2.setDetail("iphone6苹果手机!");
        
        itemList.add(items_1);
        itemList.add(items_2);
        
        // 模型和视图
        // model模型:模型对象中存放了返回给页面的数据
        // view视图:视图对象指定了返回页面的位置
        ModelAndView modelAndView = new ModelAndView();
        // 添加model
        modelAndView.addObject("itemList", itemList);
        // 添加view
        modelAndView.setViewName("itemList");
        return modelAndView;
    }
}

 

4、编写springmvc核心配置文件

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:dubbo="http://code.alibabatech.com/schema/dubbo"
    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://code.alibabatech.com/schema/dubbo 
        http://code.alibabatech.com/schema/dubbo/dubbo.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.itheima.controller"></context:component-scan>
    
    <!-- 如果没有显式地配置处理器映射器和处理器适配器那么springmvc会去默认的dispatchServlet.properties中查找,
        对应的处理器映射器和处理器适配器去使用,这样每个请求都要扫描一次它到默认配置文件,效率非常低,会降低访问速度,所以要显式地配置
        处理器映射器和处理器适配器
     -->
     <!-- 配置最新版的注解的处理器映射器 -->
     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
     <!-- 配置最新版的注解的处理器适配器 -->
     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
    
     <!-- 注解驱动:自动配置最新版的处理器映射器和处理器适配器,不用再写上面两个配置,实际开发中使用这种方式最合适 -->
     <!--  <mvc:annotation-driven></mvc:annotation-driven> -->
     
     <!-- 配置视图解析器
         作用:在controller中指定页面路径的时候就不用写完整的页面路径
      -->
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix" value="/WEB-INF/jsp/"></property>
         <property name="suffix" value=".jsp"></property>
     </bean>
</beans>

 5、测试

技术分享图片

 

整个项目结构如下:

技术分享图片

 

SpringMVC环境搭建

标签:source   imp   cpp   udf   beans   padding   otf   pve   dad   

原文地址:https://www.cnblogs.com/binaway/p/9185470.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!