Maven工程的SpringMVC结构的Web程序

来源:互联网 发布:monit windows agent 编辑:程序博客网 时间:2024/05/16 09:26

背景

之前一直在别人创建好的Web应用下写if-else,突然发现自己连创建一个SpringMVC结构的Web应用都要查半天。
所以,此篇特记录下,如何创建一个用Maven工程创建一个HelloWorld的web应用。

做法

创建一个Maven的Web项目

我用的IDE是IDEA,这里,先用IDEA创建一个Maven的Web项目。

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

添加修改配置文件

这里写图片描述

各个文件的内容:
1. pom.xml中引入依赖
2. web.xml中添加servlet(此时,spring-config-mvc.xml中还没有内容)
3. spring-config-mvc.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: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.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">    <!-- 0.上面的那些头可以官网下载 -->    <!-- 配置spring -->    <!-- 1:开启SpringMVC注解模式 -->    <!-- 简化配置:        (1)自动注册DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter        (2)提供一系列:数据绑定,数字和日期的format @NumberFormat,@DataTimeFormat,xml,json默认读写支持    -->    <mvc:annotation-driven/>    <!-- 2:servlet-mapping 映射路径:"/" -->    <!-- 静态资源默认servlet配置        1.加入对静态资源的处理        2.欲需使用"/"做整体映射     -->    <mvc:default-servlet-handler/>    <!-- 3:配置jsp 显示ViewResolver -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>        <property name="prefix" value="/WEB-INF/jsp/"/>        <property name="suffix" value=".jsp"/>    </bean>    <!-- 4:扫描web相关的bean -->    <context:component-scan base-package="org.test.*" /></beans>

4.这个配置文件的第四步中,此时还没有对应的controller类
5.在org.test.controller包下新建HelloWorld类

package org.test.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/hello")public class HelloWorld {    @RequestMapping("")    public String hello(){        return "hello";    }}

6.第5步的HelloWorld类中,这个/hello还没有对应的页面
7.新建hello.jsp页面

完成!
完整练习Demo:https://github.com/wayss000/PracticeCode/tree/master/helloweb

原创粉丝点击