初识

在web.xml 中注册DispatcherServlet

    <servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 关联一个springmvc配置文件 xxx-servlet.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <!-- /匹配所有的请求 不包括.jsp-->
<!-- /*匹配所有的请求 包括.jsp-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

在资源包中添加springmvc-servlet.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 匹配url去处理-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceView">
<!-- 前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后缀-->
<property name="suffix" value=".jsp"/>
<!-- 所以到时我们转发的时候只需给核心名字就行,避免写路径-->
</bean> <bean id="/hello" class="com.ljm.controller.HelloController"/> </beans>

HelloController类

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class HelloController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView();
mv.addObject("msg","HelloSpringMVC");
mv.setViewName("hello"); //WEB-INF/jsp/hello.jsp
return mv;
}
}

启动失败的话,若是代码无问题 显示404什么的可以看下IDEA的项目发布中,有没有添加lib有关依赖

添加后,在maven里面clean下生命周期 , 重启tomcat 就行

--------------------------------------------------------------------------------------------------------------------------------------------

使用注解开发

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"> <servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 关联一个springmvc配置文件 xxx-servlet.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <!-- /匹配所有的请求 不包括.jsp-->
<!-- /*匹配所有的请求 包括.jsp-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
"> <!-- 自动扫描包,指定包下的注解生效,由IOC容器统一管理-->
<context:component-scan base-package="com.ljm.controller"/>
<!-- 让SpringMVC不处理静态资源 css,js,html,mp3 4-->
<mvc:default-servlet-handler/>
<!-- 自动完成之前bean的两个实例注入-->
<mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceView">
<!-- 前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后缀-->
<property name="suffix" value=".jsp"/>
<!-- 所以到时我们转发的时候只需给核心名字就行,避免写路径-->
</bean> </beans>

controller

@Controller   //完成之前bean的注册作用
//@RequestMapping("/hello")
//@RequestMapping可以作用于类 也可以作用于方法 一般选后者 两者都有的话url地址要叠加
public class HelloController { @RequestMapping("/hello") //真实访问地址 项目名/*/hello
public String hello666(Model model){
//封装数据
model.addAttribute("msg","hello anno");
return "jojohello"; //会被视图解析器处理 jsp目录下的jsp文件
} }

JSON格式返回的话用注解@RestController

视图(.jsp)可以被复用

重定向与转发

SpringMVC 配置 & 初识 & 注解 &重定向与转发的更多相关文章

  1. SpringMVC Controller之间的重定向和转发

    同一个controller之间重定向和转发 ①redirect 在Controller的映射方法中,其返回值改为:return "redirect:XXX"; ②forward 这 ...

  2. Springmvc配置定时任务注解开发

    1.添加命名空间和xsd约束 xmlns:task="http://www.springframework.org/schema/task" http://www.springfr ...

  3. SpringMVC(一) 简单代码编写,注解,重定向与转发

    SpringMVC是什么 SpringMVC是目前最好的实现MVC设计模式的框架,是Spring框架的一个分支产品,以SpringIOC容器为基础,并利用容器的特性来简化它的配置.SpringMVC相 ...

  4. SpringMVC系列(九)自定义视图、重定向、转发

    一.自定义视图 1. 自定义一个视图HelloView.java,使用@Component注解交给Spring IOC容器处理 package com.study.springmvc.views; i ...

  5. SpringMVC 返回 html 视图页面,SpringMVC与Servlet,Servlet重定向与转发

    1. SpringMVC与Servlet的关系 SpringMVC框架是建立在Servlet之上的,提供各种功能,各种封装,各种方便的同时,它一点儿也没有限制Servlet,我们完全可以在Spring ...

  6. SpringMVC基础配置(通过注解配置,非xml配置)

    SpringMVC是什么,有多火,我这里就不再啰嗦了,SpringMVC比Struts2好用太多,我在学校的时候私下里两种都接触过,对比之后果断选择了SpringMVC,后来在做Android应用开发 ...

  7. springMVC中接收请求参数&&数据转发

    ### 1. 接收请求参数 #### 1.1. [不推荐] 通过HttpServletRequest获取请求参数 假设存在: <form action="handle_login.do ...

  8. SpringMVC 配置和请求方式

    SpringMVC 总结内容 一.什么是 Spring MVC ? Spring MVC 是 Spring 对 MVC 思想的实现(三层架构) 优点: 二.前端控制器 Spring MVC 中的前端控 ...

  9. SpringMVC配置实例

    一.SpringMVC概述 MVCII模式实现的框架技术 Model--业务模型(Biz,Dao...) View--jsp及相关的jquery框架技术(easyui) Contraller--Dis ...

随机推荐

  1. 如何使用 Spring Boot 实现分页和排序?

    使用 Spring Boot 实现分页非常简单.使用 Spring Data-JPA 可以实现将可分页的传递给存储库方法.

  2. Homebrew 卸载后重新安装mysql

    1.卸载https://blog.csdn.net/liuxw1/article/details/81434005 https://jingyan.baidu.com/article/5553fa82 ...

  3. spring中bean的五种作用域?Spring中的bean是线程安全的吗?

    spring中bean的五种作用域 当通过spring容器创建一个Bean实例时,不仅可以完成Bean实例的实例化,还可以为Bean指定特定的作用域.Spring支持如下5种作用域: singleto ...

  4. MySQL怎么用命令修改字段名

    一.SQL MySQL怎么用命令修改字段名的 -- alter table 表名 change 旧属性 新属性 新的数据类型 ==>(可以不写) COMMENT '备注' ALTER TABLE ...

  5. CommonCollection1反序列化学系

    CommonsCollection1 1.前置知识 1.1.反射基础知识 1.1.1. 对象与类的基础知识 类(class),对象(object) 对象是类的实例化,中华田园犬(object)是狗(c ...

  6. 深入理解FIFO(包含有FIFO深度的解释)

    FIFO: 一.先入先出队列(First Input First Output,FIFO)这是一种传统的按序执行方法,先进入的指令先完成并引退,跟着才执行第二条指令. 1.什么是FIFO? FIFO是 ...

  7. (stm32f103学习总结)—RTC独立定时器—实时时钟实验

    一.STM32F1 RTC介绍 1.1 RTC简介 STM32 的实时时钟( RTC)是一个独立的定时器. STM32 的 RTC 模 块拥有一组连续计数的计数器,在相应软件配置下,可提供时钟日历的 ...

  8. 【静态页面架构】CSS之定位

    CSS架构 1.浮动: 是以float属性设置容器指定的位置 <style> div { width: 200px; height: 200px; } #qq { background-c ...

  9. (SSM框架)实现小程序图片上传(配小程序源码)

    阅读本文约"2分钟" 又是一个开源小组件啦! 因为刚好做到这个小功能,所以就整理了一下,针对微信小程序的图片(文件)上传! 原业务是针对用户反馈的图片上传.(没错,本次还提供小程序 ...

  10. 体温填报app作业演示

    今日学习 今天把这个体温填写app作业,做完了. 具体开发流程:https://www.cnblogs.com/yuxuan-light-of-Taihu-Lake/p/14362107.html 点 ...