SpringMVC框架搭建的步骤

1.导包(sping系列的包的版本号要尽量一样,否则可能会报错)

2.配置web.xml

 

<servlet>
  	<servlet-name>mvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        
       <!-- 寻找文件 -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc.xml</param-value><!-- 文件名是下一步创建的xml -->
  	</init-param>  	
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>mvc</servlet-name>
  	<!-- 除了jsp之外的所有请求资源 -->
  	<url-pattern>/</url-pattern>
  </servlet-mapping>

3.在src下配置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: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">

       <!-- 扫描注解 @Controller--> 
       <context:component-scan base-package="com.bj.controller"></context:component-scan>
		
	<!-- 扫描 @RequestMapping -->
	<mvc:annotation-driven></mvc:annotation-driven>
	   
	   <!-- 静态资源的放行 -->
	   <!-- 
	   	   location:指代的是放行本地的什么资源
	   	   mapping:指代的是网络的地址 
	   -->
	   <mvc:resources location="/img/" mapping="/img/**"></mvc:resources>
	   <mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
	   <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
	   
</beans>

附:使用静态资源(js、css、img 等)放行的时候有两种方式

方式1:配置web.xml中使用url-pattern方式

<servlet-mapping>
  <servlet-name>mvc</servlet-name>
  <url-pattern>*.do</url-pattern>
  <url-pattern>*.action</url-pattern>
 </servlet-mapping>
<!-- 这样配置就需要下一步的@RequestMapping("abc")变为@RequestMapping("abc.do")或@RequestMapping("abc.action")-->

方式2:springmvc.xml中进行放行配置(一般用这种方式)

<!--
     location 元素表示webapp或者web目录下的img包下的所有文件
     mapping 元素表示以/img开头的所有请求路径,如/img/a.jpg
-->
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
<mvc:resources location="/img/" mapping="/img/**"></mvc:resources>

4.书写自己的控制单元方法

@Controller
public class MyContro {

	@RequestMapping("abc")
	public String demo1() {
		//1.接受页面数据
		
		//2.数据处理
		System.out.println("进入了demo1控制单元");
		//3.做出响应
		return "index.jsp";//这是请求转发的方式
	}
}

@Controller用于标记在一个类上,使用它标记的类就是一个控制器类

@RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

index.jsp代码

<body>
	<h1>index.jsp</h1>
	<img src="img/a.jpg">
</body>

请求路径

 

网页运行结果

控制台运行结果

理解示意图

 

有帮到你的点赞、收藏一下吧

                                                                      需要更多教程,微信扫码即可

                                                                                 

                                                                                         👆👆👆

                                                        别忘了扫码领资料哦【高清Java学习路线图】

                                                                     和【全套学习视频及配套资料】
 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值