spring入门(三) 使用spring mvc
2018-10-03 17:57:37来源:博客园 阅读 ()
1.建立project / module
新建空的project:springMvcStudy
新建module:type maven-webapp,名字mvcStudy
2.为module设置Sources和Resources
在mvcStudy/src/main下新建2个文件夹:java,resources.
打开File/Project Structure/Project Settings/Modules 选择mvcStudy,点击Sources选项卡
设置java文件夹为Sources,设置resources为Resources.
3.修改pom,增加依赖:
1 <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> 2 <dependency> 3 <groupId>org.springframework</groupId> 4 <artifactId>spring-core</artifactId> 5 <version>5.0.9.RELEASE</version> 6 </dependency> 7 <dependency> 8 <groupId>org.springframework</groupId> 9 <artifactId>spring-context</artifactId> 10 <version>5.0.9.RELEASE</version> 11 </dependency> 12 <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> 13 <dependency> 14 <groupId>org.springframework</groupId> 15 <artifactId>spring-beans</artifactId> 16 <version>5.0.9.RELEASE</version> 17 </dependency> 18 <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression --> 19 <dependency> 20 <groupId>org.springframework</groupId> 21 <artifactId>spring-expression</artifactId> 22 <version>5.0.9.RELEASE</version> 23 </dependency> 24 25 <dependency> 26 <groupId>org.springframework</groupId> 27 <artifactId>spring-web</artifactId> 28 <version>5.0.9.RELEASE</version> 29 </dependency> 30 <dependency> 31 <groupId>org.springframework</groupId> 32 <artifactId>spring-webmvc</artifactId> 33 <version>5.0.9.RELEASE</version> 34 </dependency> 35 <dependency> 36 <groupId>org.springframework</groupId> 37 <artifactId>spring-aop</artifactId> 38 <version>5.0.9.RELEASE</version> 39 </dependency>
4.Web.xml:增加SpringMVC的配置
1 <!--configure the setting of springmvcDispatcherServlet and configure the mapping--> 2 <servlet> 3 <servlet-name>springmvc</servlet-name> 4 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 5 <init-param> 6 <param-name>contextConfigLocation</param-name> 7 <param-value>classpath:springmvc-config.xml</param-value> 8 </init-param> 9 <!-- <load-on-startup>1</load-on-startup> --> 10 </servlet> 11 12 <servlet-mapping> 13 <servlet-name>springmvc</servlet-name> 14 <url-pattern>/</url-pattern> 15 </servlet-mapping>
5.在resources文件下建立 springmvc-config.xml,内容如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 11 "> 12 <!--注意配置xsd,要么报错:通配符的匹配很全面, 但无法找到元素 'context:component-scan' 的声明。--> 13 14 <context:component-scan base-package="com.ice"/> 15 <!-- don't handle the static resource --> 16 <mvc:default-servlet-handler /> 17 <mvc:annotation-driven/> 18 19 <!-- configure the InternalResourceViewResolver --> 20 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 21 id="internalResourceViewResolver"> 22 <!-- 前缀 --> 23 <property name="prefix" value="/WEB-INF/jsp/" /> 24 <!-- 后缀 --> 25 <property name="suffix" value=".jsp" /> 26 </bean> 27 28 </beans>
6.创建包,类
上面定义的扫描包 base-package="com.ice".
因此,建立 package com.ice.controller; 测试类如下:
1 package com.ice.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 @RequestMapping("/") 7 @Controller 8 public class HomeController { 9 @RequestMapping("/") 10 public String index(){ 11 return "index"; 12 } 13 }
7.确认 WEB-INF/jsp下有index.jsp文件.
8.发布tomcat
点击run->run,选择edit configure,点击绿色'+',增加tomcat server |--local,
a.点击server选项卡:配置application server为本地tomcat server.
b.点击deployment选项卡:点击绿色'+',选择artifact..,选择模式 war exploded模式 (修改文件后自动更新到tomcat.)
war模式:将WEB工程以包的形式上传到服务器 ;
war exploded模式:将WEB工程以当前文件夹的位置关系上传到服务器;
c.点击server选项卡:VM options:下边的下拉可以改为 update resources
9.运行
点击run,自动访问 http://localhost:port/
显示 index.jsp的内容:hello world.
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Spring系列.ApplicationContext接口 2020-06-11
- springboot2配置JavaMelody与springMVC配置JavaMelody 2020-06-11
- 给你一份超详细 Spring Boot 知识清单 2020-06-11
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- 掌握SpringBoot-2.3的容器探针:实战篇 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash