本项目是SpringMVC的入门项目,用于演示SpringMVC的项目配置、各层结构,功能较简单


一、Eclipse中创建maven项目

二、pom.xml添加依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.2</version>
</dependency>
 
<!--① 依赖的Spring模块类库 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>3.2.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>3.2.3.RELEASE</version>
</dependency>

三、web.config配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    <display-name>SpringMVC</display-name>
     
    <!-- Spring应用上下文, 理解层次化的ApplicationContext -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
     
     
    <!-- DispatcherServlet, Spring MVC的核心 -->
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- DispatcherServlet对应的上下文配置, 默认为/WEB-INF/$servlet-name$-servlet.xml -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <!-- mvc-dispatcher拦截所有的请求 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

四、添加applicationContext.xml配置文件 
        文件路径对应web.xml中<context-param><param-value>节点值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8"?>
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     
    <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
    <context:component-scan base-package="com.james"/>
</beans>

五、添加mvc-dispatcher-servlet.xml配置文件

        文件路径对应web.xml中<servlet><init-param><param-value>节点值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml version="1.0" encoding="UTF-8"?>
    xsi:schemaLocation="
    <context:component-scan base-package="com.james">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
 
    <!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 -->
    <mvc:annotation-driven />
 
    <!-- 静态资源处理, css, js, imgs -->
    <mvc:resources mapping="/resources/**" location="/resources/" />
 
    <!-- /WEB-INF/views/ 对应jsp文件路径 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

六、添加web、service、dao层及类

1、HelloController.java  包自定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.web.controller;
 
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.james.service.HelloService;
 
@Controller
@RequestMapping("/hello")
public class HelloController {
 
    @Autowired
    private HelloService service;
     
    @RequestMapping("/mvc")
    public String helloMvc(HttpSession httpSession) {
 
        System.out.println("进入:HelloController-->helloMvc");
        this.service.helloMvc();
         
        // 视图渲染,/WEB-INF/views/home.jsp
        return "home";
    }
}

2、HelloService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.service;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.dao.HelloDao;
 
@Service
public class HelloService {
     
    @Autowired
    private HelloDao dao;
     
    public void helloMvc() {
        System.out.println("进入:HelloService-->helloMvc");
         
        this.dao.helloMvc();
    }
}

3、HelloDao.java

1
2
3
4
5
6
7
8
9
10
11
12
package com.dao;
 
import org.springframework.stereotype.Repository;
 
@Repository
public class HelloDao {
    public void helloMvc() {
        System.out.println("进入:HelloDao-->helloMvc");
         
        System.out.println("进入:HelloDao-->helloMvc2");
    }
}

七、利用jetty启动mvc并查看输出



附件列表

SpringMVC_入门项目的更多相关文章

  1. kaggle入门项目:Titanic存亡预测 (一)比赛简介

    自从入了数据挖掘的坑,就在不停的看视频刷书,但是总觉得实在太过抽象,在结束了coursera上Andrew Ng 教授的机器学习课程还有刷完一整本集体智慧编程后更加迷茫了,所以需要一个实践项目来扎实之 ...

  2. bootstrap入门项目备份

    bootstrap入门项目备份 http://files.cnblogs.com/files/wordblog/bootstrap%E5%85%A5%E9%97%A8%E9%A1%B9%E7%9B%A ...

  3. 【CuteJavaScript】Angular6入门项目(1.构建项目和创建路由)

    本文目录 一.项目起步 二.编写路由组件 三.编写页面组件 1.编写单一组件 2.模拟数据 3.编写主从组件 四.编写服务 1.为什么需要服务 2.编写服务 五.引入RxJS 1.关于RxJS 2.引 ...

  4. Spring学习随笔(2):Eclipse下Spring环境配置+入门项目

    1 准备工作 (按需下载) Eclipse 下载:http://www.eclipse.org/downloads/eclipse-packages/ : Spring 下载:http://repo. ...

  5. 入门项目数字手写体识别:使用Keras完成CNN模型搭建(重要)

    摘要: 本文是通过Keras实现深度学习入门项目——数字手写体识别,整个流程介绍比较详细,适合初学者上手实践. 对于图像分类任务而言,卷积神经网络(CNN)是目前最优的网络结构,没有之一.在面部识别. ...

  6. SpringCloud学习之手把手教你用IDEA搭建入门项目(三)

    本篇博客是承接上一篇<手把手教你用IDEA搭建SpringCloud入门项目(二)>,不清楚的请到我的博客空间查看后再看本篇博客,上面两篇博客成功创建了一个简单的SpringCloud项目 ...

  7. SpringCloud学习之手把手教你用IDEA搭建入门项目(二)

    本篇博客是承接上一篇<手把手教你用IDEA搭建SpringCloud入门项目(一)>,不清楚的请到我的博客空间查看后再看本篇博客 1)先创建一个Eureka服务注册中心模块,用来作为服务的 ...

  8. SpringCloud学习之手把手教你用IDEA搭建入门项目(一)

    SpringCloud简单搭建 jdk:1.8开发工具:IDEA注:需要了解springcloud 1.创建最简单的Maven项目 1)开始创建一个新的项目 ​ 2)创建一个空模板的maven项目,用 ...

  9. mnist手写数字识别——深度学习入门项目(tensorflow+keras+Sequential模型)

    前言 今天记录一下深度学习的另外一个入门项目——<mnist数据集手写数字识别>,这是一个入门必备的学习案例,主要使用了tensorflow下的keras网络结构的Sequential模型 ...

随机推荐

  1. Java将CST的时间字符串转换成需要的日期格式字符串

    已知得到的Date类型的变量meettingdate 的值为Sun Dec 16 10:56:34 CST :现在要将它改为yyyy-MM-dd类型或yyyy年MM月dd日: 变为yyyy年MM月dd ...

  2. App推广

    一行业常见名词解释 开发商:也叫CP,即Content Provider内容提供商的英文首字母缩写. 发行商(运营商):即代理CP开发出来的产品. 渠道:拥有用户,能够进行流量分发的公司,即可成为渠道 ...

  3. MiCode 40: 找小“3”

    题目链接 这道题真的是zjb恶心, 看其起来像是个数位dp, 然而我并不会数位dp.然后就xjb乱写了个雷类似于动态规划的玩意, 然后调出了\(9\times 9 = 81\)种Bug, 终于过了. ...

  4. http协议及http协议和tcp协议的区别

    http是应用层的协议,并且无连接,无状态的协议. http协议的特点: 1.支持c/s模式 2.简单快速:客户端向服务器端传送数据的时候,只需要发送请求方法和路径,请求方法有:post,get,he ...

  5. web请求响应

    转载自:SanMaoSpace 1.Web开发的定义首先看看微软对Web开发的定义:Web开发是一个指代网页或网站编写过程的广义术语.网页使用 HTML.CSS 和 JavaScript编写.这些页面 ...

  6. hdu 3367(与最大生成树无关。无关。无关。重要的事情说三遍+kruskal变形)

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  7. swiper使用心得

    引入: <link rel="stylesheet" href="https://cdn.bootcss.com/Swiper/3.4.2/css/swiper.m ...

  8. Codeforces 1082 C. Multi-Subject Competition-有点意思 (Educational Codeforces Round 55 (Rated for Div. 2))

    C. Multi-Subject Competition time limit per test 2 seconds memory limit per test 256 megabytes input ...

  9. socket编程(TCP)

    1.模型:(图片来自百度百科) 服务端: 1)创建socket对象 int socket(int domain, int type, int protocol) domain:即协议域,又称为协议族( ...

  10. 【前端必备】一、HTML篇

    1.文档类型是什么概念,起什么作用?    <!DOCTYPE> 声明此标签可告知浏览器文档使用哪种 HTML 或 XHTML 规范.   该标签可声明三种 DTD 类型,分别表示严格版本 ...