SpringMVC——SpringMVC 的入门案例

1.建立web 项目,导入SpringMVC 相关支持jar 包

commons-logging-1.2.jar下载地址:https://commons.apache.org/proper/commons-logging/download_logging.cgi

2.配置web.xml(重点)

关键的步骤:配置核心控制器:Servlet

 1   <!-- 配置SpringMVC核心控制器 -->
 2   <servlet>
 3       <servlet-name>DispatcherServlet</servlet-name>
 4       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 5   </servlet>
 6   
 7   <servlet-mapping>
 8       <servlet-name>DispatcherServlet</servlet-name>
 9       <url-pattern>*.action</url-pattern>
10   </servlet-mapping>

3.编写spring-mvc.xml

建议放在classpath

下面有一个mvc 的名称空间:

SpringMVC的完整配置:

 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-4.3.xsd
 8                         http://www.springframework.org/schema/context 
 9                         http://www.springframework.org/schema/context/spring-context-4.3.xsd
10                         http://www.springframework.org/schema/mvc 
11                         http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
12 
13     <!-- 扫描Spring注解 -->
14     <context:component-scan base-package="cn.sm1234.controller"></context:component-scan>
15     
16 </beans>

修改web.xml

 1   <!-- 配置SpringMVC核心控制器 -->
 2   <servlet>
 3       <servlet-name>DispatcherServlet</servlet-name>
 4       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 5   <!-- 参数,用于加载spring-mvc.xml -->
 6       <init-param>
 7           <param-name>contextConfigLocation</param-name>
 8           <param-value>classpath:spring-mvc.xml</param-value>
 9       </init-param>
10   </servlet>
11   
12   <servlet-mapping>
13       <servlet-name>DispatcherServlet</servlet-name>
14       <url-pattern>*.action</url-pattern>
15   </servlet-mapping>

4.编写Controller 类测试

 1 package cn.sm1234.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.servlet.ModelAndView;
 6 
 7 @Controller
 8 public class HelloController {
 9 
10     @RequestMapping("/hello")
11     public ModelAndView hello(){
12         System.out.println("执行 HelloController的 hello方法");
13         //把数据保存到 ModelAndView(相当于保存 request域对象)
14         ModelAndView mv = new ModelAndView();
15         mv.addObject("name", "Spring MVC");
16         //返回物理路径
17         mv.setViewName("/WEB-INF/jsp/success.jsp");
18         return mv;
19     }
20 }

5.编写页面

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%
 4     String path = request.getContextPath() + "/";
 5     String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;
 6 %>
 7 <!DOCTYPE html>
 8 <html>
 9 <head>
10 <base href="<%=basePath%>" />
11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
12 <title>第一个SpringMVC程序</title>
13 </head>
14 <body>
15 第一个SpringMVC程序
16 <hr>
17 ${name }
18 </body>
19 </html>

6.访问测试

访问路径:http://localhost:8080/ch01.spring-mvc/hello.action

结果:

浏览器:

控制台:

 

转载于:https://www.cnblogs.com/116970u/p/10193054.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值