使用idea创建spring +spring mvc 的 maven项目(教学)

电脑系统:windows 10

本机java相关的环境 : jdk 1.8 ,tomcat 8.5 ,maven 3.5.4 ,idea2017

 

下面,打开idea,开始新建项目

 

打开idea,file-new-project

使用idea创建spring +spring mvc 的 maven项目(教学)_第1张图片

勾上创建模板的选项,选择maven-archetype-webapp

 

使用idea创建spring +spring mvc 的 maven项目(教学)_第2张图片

点击next

 

填写groupid 和artifactId ,这里说明一下,groupId一般是自己机构的名称,artifactId可以看作是项目的名称。填好后点击next

 使用idea创建spring +spring mvc 的 maven项目(教学)_第3张图片

maven环境的一些选项,如下图

使用idea创建spring +spring mvc 的 maven项目(教学)_第4张图片

点击next

选择项目名字以及项目存放的位置

使用idea创建spring +spring mvc 的 maven项目(教学)_第5张图片

选择好后点击finish

 

根据我们选择的maven-archetype-webapp模板,会生成一下目录结构

使用idea创建spring +spring mvc 的 maven项目(教学)_第6张图片

在webapp的同级目录新建两个文件夹,java(用来放源代码),resources(用来放资源配置文件)

使用idea创建spring +spring mvc 的 maven项目(教学)_第7张图片

如上图,对着java文件夹点击鼠标右键,将它标记为sources root 即源代码的根目录

将resources文件夹标记为resources root

 

 

点击右上角的三角形,如下图,点击edit..

 

使用idea创建spring +spring mvc 的 maven项目(教学)_第8张图片

添加tomcat 服务器

使用idea创建spring +spring mvc 的 maven项目(教学)_第9张图片

使用idea创建spring +spring mvc 的 maven项目(教学)_第10张图片

选择deployment

使用idea创建spring +spring mvc 的 maven项目(教学)_第11张图片

点击ok,点击右上角的运行,成功运行则会出现hello word 欢迎页面

使用idea创建spring +spring mvc 的 maven项目(教学)_第12张图片

使用idea创建spring +spring mvc 的 maven项目(教学)_第13张图片

现在,一个普通的web环境就已经搭建好了,现在我们要加入 spring,spring mvc配置

 

将spring的相关依赖添加到pom.xml中,让maven去远程仓库中下载相关jar包

参考依赖如下:

 


    

      junit

      junit

      4.12

      test

    

    

    

      org.slf4j

      slf4j-log4j12

      1.7.21

    

    

    

      javax.servlet

      javax.servlet-api

      3.1.0

    

    

      javax.servlet.jsp

      jsp-api

      2.2

    

    

      javax.servlet

      jstl

      1.2

    

    

    

      mysql

      mysql-connector-java

      5.1.35

    

    

    

      org.springframework

      spring-web

      4.2.6.RELEASE

    

    

      org.springframework

      spring-webmvc

      4.2.6.RELEASE

    

    

      org.springframework

      spring-context

      4.2.6.RELEASE

    

    

      org.springframework

      spring-test

      4.2.6.RELEASE

    

      

          org.springframework

          spring-jdbc

          4.2.6.RELEASE

      

    

      com.github.stefanbirkner

      system-rules

      1.16.1

      test

    

    

      org.aspectj

      aspectjweaver

      1.8.9

    

    

    

        org.apache.commons

        commons-lang3

        3.4

    

    

        commons-fileupload

        commons-fileupload

        1.3.1

    





com.fasterxml.jackson.core

jackson-core

2.8.5





com.fasterxml.jackson.core

jackson-databind

2.8.5





com.fasterxml.jackson.core

jackson-annotations

2.8.5



 

 

 

 

Spring jar包依赖下载好以后,接下来添加spring的配置文件

使用idea创建spring +spring mvc 的 maven项目(教学)_第14张图片

 

 使用idea创建spring +spring mvc 的 maven项目(教学)_第15张图片

使用idea创建spring +spring mvc 的 maven项目(教学)_第16张图片

 

 

点击ok,可以看到多了两个配置文件

使用idea创建spring +spring mvc 的 maven项目(教学)_第17张图片

Web.xml中也会增加一些配置

使用idea创建spring +spring mvc 的 maven项目(教学)_第18张图片

 

 

到现在,我们的spring,spring mvc就已经配置好了

 

 

测试一下 dispatcher servlet转发请求

 

增加TestController类,和一个test.jsp文件,目录结构如图

 

使用idea创建spring +spring mvc 的 maven项目(教学)_第19张图片

dispatcher-servlet.xml 配置如下




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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd">



package="com.sicau.controller"/>







allowed-origins="*"

allowed-methods="POST,GET,OPTIONS,DELETE,PUT"

allowed-headers="Content-Type,Access-Control-Allow-Headers,Authorization,X-Requested-With"

allow-credentials="true"/>















text/plain;charset=UTF-8

text/html;charset=UTF-8













text/html;charset=UTF-8

text/plain;charset=UTF-8

application/json;charset=UTF-8











 



class="org.springframework.web.servlet.view.InternalResourceViewResolver">







 

 

 

Test.jsp中的代码如图

使用idea创建spring +spring mvc 的 maven项目(教学)_第20张图片

运行程序,浏览器输入http://localhost:8080/toTest.form,成功跳转到test页面

 使用idea创建spring +spring mvc 的 maven项目(教学)_第21张图片

控制台打印出“进入toTest方法”字样

 

由于web.xml自动生成的是servlet 2.3版本的配置,而我们项目中需要用servlet 3.0的一些东西。所以需要改一下web.xml servlet版本

使用idea创建spring +spring mvc 的 maven项目(教学)_第22张图片

 

 

更改的地方代码如下:

 



xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

 

 

检查是否生效

返回test页面的同时,返回一个数据,name

使用idea创建spring +spring mvc 的 maven项目(教学)_第23张图片

 

 

在test.jsp页面中 ,使用el表达式访问name数据

使用idea创建spring +spring mvc 的 maven项目(教学)_第24张图片

el表达式需要servlet 3.0支持,所以可以用来测试

 

运行服务器,浏览器访问如下图,可以正确显示name的数据则没有问题

 使用idea创建spring +spring mvc 的 maven项目(教学)_第25张图片

 

转载于:https://www.cnblogs.com/cavinchen/p/9846427.html

你可能感兴趣的:(测试,java,开发工具)