SpringMVC4.3x教程之一基本使用详解

我们上次说了Spring的使用,那么接下来我们来聊聊SpringMVC的使用。Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。可以对三层中的显示层进行进一步的分离,实现解耦操作。当然不可否则SpringMVC自带了很多功能可以帮助我们简化Web的开发。
SpringMVC的底层基于Servlet进行的实现,所以在使用的时候需要配置Servlet.
整体来说使用步骤:
1、引用jar包或使用Maven依赖jar包
2、配置web.xml,主要就是实现Servlet的配置
3、创建控制器
可以选择实现接口或者通过注解实现
4、创建SpringMVC的配置
5、测试

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>SpringMVC_1</display-name>

  <!--SpringMVC的调度Servlet,前端控制器  -->
  <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!--初始化参数设置,加载配置文件  -->
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <!--配置文件的路径,如果不写,默认加载的路径:WEB-INF/servlet-name-servlet.xml  -->
  <param-value>classpath:springMVC.xml</param-value>
  </init-param>
  <!--启动的优先级 值越小越先启动,>0 -->
  <load-on-startup>1</load-on-startup>
  </servlet>
  <!--映射  -->
  <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <!--url的匹配规则-->
  <url-pattern>/</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

控制器类:

@Controller//这是一个控制器
public class StudyController {

    @RequestMapping(value="/study.do")
    public String test1(Model model,HttpServletRequest request){
        System.out.println("请求来了");
        //DispatcherServlet
        //想回复数据
        model.addAttribute("s1", "你要做什么,为什么?");
        request.setAttribute("s1", "吃饭吗");
        System.out.println("s1:"+request.getAttribute("s1"));
        return "index.jsp";
    }
}

看看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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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">

    <!--第一种方式:基于接口实现控制器 -->
    <!--IOC创建对象,如果是控制器,id或name的名称前面需要加/  -->
   <!--<bean id="/hello" class="cn.code404.web.controller.HelloController"></bean>-->
    <!--第二种方式:基于注解实现控制器  -->
    <!--扫描带注解的类:@Controller  -->
    <context:component-scan base-package="cn.code404.web.controller"></context:component-scan>
</beans>

在页面中

<a href="hello">快来请求MVC的控制器</a>
<h1>${msg }</h1>
<a href="study.do">控制器的第二种创建方式</a>

官方流程图

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值