springMvc初体验

SpringMvc初体验

SpringMvc框架对我来说有一种很奇怪的吸引,第一次尝试着去自己写一个简单的初次体验SpringMvc的一些基础的知识点,这里做上笔记。

初次体验就让我感觉到一个框架的美丽,那么我们去写一个Demo之前,我们需要去准备关于SpringMvc的相关jar我这里就不列举了。

先我们看一下,基础的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:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<!-- 
		开启注解扫描
		其实当我们使用全注解开发时这里是可以完全省去的,使用 @Configuration 即可解决所有的问题了
 	-->
	<context:component-scan base-package="cn.shaojie"/>
	
	<!-- 配置springMvc的注解扫描 -->
	<mvc:annotation-driven/>
	
	<!-- 视图解析器 注意这个类所存在的 jar spring-webmvc-版本号.jar -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 前缀 自己的静态资源存放的文件夹位置 -->
		<property name="prefix" value="/WEB-INF/static/" />
		<!-- 后缀 个人习惯去用 HTMl 而不是说只能用 HTML-->
		<property name="suffix" value=".html"></property>
	</bean>

</beans>

别的不说,我觉得我这个像最全的SpringMvc.xml配置了,拿去不谢!

当我们将SpringMvc.xml配置完成后,还有一个需要配置,那就是web.xml,别的不说,这个xml文件是真的多(小小吐槽一下,不要介意),废话不多说,快看一下这个怎么配置吧!

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  
   <!-- 欢迎页面 -->
  <welcome-file-list>
  	<welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 
  		springMvc 的监听
  		每次请求都会被解析到 org.springframework.web.servlet.DispatcherServlet
   -->
  <servlet>
		<servlet-name>springMvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<!-- 部署就开始加载  这个值不能低于1 否则相当于没有设置-->
		<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
		<servlet-name>springMvc</servlet-name>
		<url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <!-- 
  		 拦截器	 拦截字符编码
   -->
  <filter>
  		<filter-name>characterEncodingFilter</filter-name>
  		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  		<init-param>
  			<param-name>encoding</param-name>
  			<param-value>UTF-8</param-value>
  		</init-param>
  </filter>
  <filter-mapping>
  		<filter-name>characterEncodingFilter</filter-name>
  		<url-pattern>*.do</url-pattern>
  </filter-mapping>
  
</web-app>

有必要说一个问题,拦截的所有请求都叫*.do为什么是这样,其实没有一个仔细的说明,这里指的应该是请求的方式吧,猜着来呗,代码多变,随便你怎么猜测。不能缺少探索的精神,我这里就遵循以下老的请求方式吧!

以上配置完成,下面就开始测试控制层的代码

@Controller
public class UserController {
	
	@RequestMapping("/index")
	public ModelAndView index(){
		// 这里是对主页面 index.html 的请求
		// 创建 ModelAndView 对象
		ModelAndView modelAndView = new ModelAndView();
		// 设置视图名称 这里的名称就好比是一个请求 请求到 名为 index 的页面
		modelAndView.setViewName("index");
		// 返回视图
		return modelAndView;
	}

}

@Controller注解是将这个类定义为控制器层,控制数据、跳转等。不可或缺。@RequestMapping("/index")注解,作用是映射处理请求的,这里对index.do做对主页面的请求,他接受到请求,创建ModelAndView 对象,又叫视图层。返回对页面的跳转。modelAndView.setViewName("index");这个就是定义页面的名称,也可以称视图的名称(其实都是废话,就是页面的名字,不要乱了),请求完毕,返回视图。

展示下基础的页面,为了体现出上面控制层的作用,回过头看一下。web.xml中配置的欢迎页面index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<% response.sendRedirect("index.do"); %>

项目一运行,访问的就是index.jsp,然后页面重定向到index.do,拦截这个请求,创建视图,跳转到

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>主页</title>
  </head>
  <body>
    	hello world springMvc!
  </body>
</html>

初次搭建springMvc到此结束

更为详细说明,请关注个人博客:https://www.lzmvlog.top/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值