springmvc入门笔记

本文介绍Spring MVC框架的基本概念,包括其组成组件如前端控制器、处理映射器等,并通过一个Hello World实例展示了如何配置和使用Spring MVC来处理HTTP请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

什么是Spring web mvc
Spring web mvc是一种基于Java的实现了web MVC设计模式的请求驱动类型的web框架,是spring框架的一个模块,是基于MVC的web框架。框架容易上手专注开发业务逻辑代码。

Spring web mvc需要了解前端控制器(Dispatchservlet)、处理映射器(HandlerMapping)、处理器适配器(HandlerAdapter)、视图解析器View resolver作用。
前端控制器:是用户request上来的url分配给谁去处理。
作用:接收请求,响应结果,相当于转发器,是springmvc的核心,主要功能调度分配 。
处理映射器:接受到前端控制器的请求url找到相应的handler,也就是说找到对应的controller的方法。例如:用户需要删除某条信息,通过(url)delete.action?id=1然后去controller找到void delete(String id)方法。

处理器适配器:执行handler,通过处理器适配器找到能执行handler的方法。
作用:按照特定规则(HandlerAdapter要求的规则)去执行Handler然后返回modeandview。

视图解析器:根据逻辑视图名生成真正的视图(在springmvc中使用View对象表示)。
作用:进行视图解析,根据逻辑视图名解析成真正的视图(view)。

View视图:jsp页面,仅是数据展示,没有业务逻辑。

Springwebmvc实例helloworld
web.xml配置 这里需要配置前端控制器DispatcherServlet
<?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_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>hello</display-name>  

    <!-- springmvc前端控制器 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器、适配器等等) 如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-serlvet.xml(springmvc-servlet.xml) -->

        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>

    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!-- 第一种:*.action,访问以.action结尾 由DispatcherServlet进行解析 第二种:/,所以访问的地址都由DispatcherServlet进行解析,对于静态文件的解析需要配置不让DispatcherServlet进行解析 
            使用此种方式可以实现 RESTful风格的url 第三种:/*,这样配置不对,使用这种配置,最终要转发到一个jsp页面时, 仍然会由DispatcherServlet解析jsp地址,不能根据jsp页面找到handler,会报错。 -->
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

springmvc.xml配置
1.配置扫描controller 扫描包
2.配置处理器,适配器 分为注解和非注解,写法不同,我使用的注解方法更简单
3.配置视图

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.2.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

<!-- 可以扫描controller、service、...
这里让扫描controller,指定controller的包
-->
<context:component-scan base-package="cn.test.hello.controller"></context:component-scan>


<!--注解映射器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->
<!--注解适配器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->

<!-- 使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置
mvc:annotation-driven默认加载很多的参数绑定方法,
比如json转换解析器就默认加载了,如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
实际开发时使用mvc:annotation-driven
-->
<mvc:annotation-driven></mvc:annotation-driven>

<!-- 视图解析器
解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包
-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置jsp路径的前缀 -->
<!--property name="prefix" value="/WEB-INF/jsp/"/-->
<!-- 配置jsp路径的后缀 -->
<property name="suffix" value=".jsp"/>
</bean>



</beans>

hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF8"
    pageEncoding="UTF8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/helloWorld.action" method="post">  <!--调用helloWorld方法-->
<td>
<input id="username" name="username" type="text"></input>
<input  type="submit" value="测试"></input>
</td>
<td>
<td>${objvalue}</td><!--后台testHello 方法 返回的值 这里显示-->
</td>
</form>
</body>
</html>

controller编写
@Controller
public class HelloController {
@RequestMapping("/helloWorld")//注解方法
public ModelAndView testHello(HttpServletRequest request) throws Exception {

System.out.println(request.getParameter("username"));//获取前端 username的值

System.out.println("1111111111111111111111111111111111");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("objvalue", "调用成功");//回显到前端显示
modelAndView.setViewName("hello");
return modelAndView;
  }
}

例子下载:http://download.youkuaiyun.com/detail/undiif123/9848025
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值