---------------------siwuxie095

 
 

 
 

 
 

 
 

 
 

 
 

 
 

SpringMVC 入门(基于注解方式实现)

 
 

 
 

SpringMVC 通过一套 MVC 注解,让 POJO 无需实现任何接口即可成

为处理请求的控制器,具体实现如下:

 
 

 
 

1、在部署描述文件中进行配置

 
 

web.xml:

 
 

<?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>TestSpringMVC</display-name>

<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>

 

 
 

<servlet>

<!-- servlet-name 可任意命名 -->

<servlet-name>dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!--

设置 SpringMVC 核心配置文件的名称和位置,均可任意。如果未设置,则默认

位于 WEB-INF 目录下,名称为 [servlet-name]-servlet.xml

-->

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:dispatcher-servlet.xml</param-value>

</init-param>

<!-- 自动加载:随 Tomcat 容器启动,加载 DispatcherServlet,完成初始化 -->

<load-on-startup>1</load-on-startup>

</servlet>

 

<servlet-mapping>

<servlet-name>dispatcher</servlet-name>

<!-- url-pattern 可以是 / 或 *.xxx 或 /xxx/*,不能是 /* -->

<url-pattern>*.do</url-pattern>

</servlet-mapping>

 
 

 

</web-app>

 
 

 
 

 
 

2、编写一个 Controller 类

 
 

HelloController.java:

 
 

package com.siwuxie095.controller;

 
 

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

 
 

/**

* 给 HelloController 加上注解 @Controller,即可变成一个 Controller

* (无需再实现 Controller 接口)

*/

@Controller

//@RequestMapping("/user")

public class HelloController {

 
 

/**

* 给方法加上 @RequestMapping 注解来设置访问路径(请求路径)

*

* @RequestMapping("/hello") 等同于 @RequestMapping(value="/hello")

*

* 当然,类也可以加上 @RequestMapping 注解,这样访问路径就多了一环。

* 如:给类上加 @RequestMapping("/user"),则访问路径为 /user/hello

*

* 另外:@RequestMapping 注解中的斜杠 / 可以省略不写,建议写上

*/

@RequestMapping("/hello")

public ModelAndView hello() {

// 创建 ModelAndView 对象,并设置视图名称

ModelAndView mv = new ModelAndView("hello");

// 添加模型数据

mv.addObject("msg", "Hello SpringMVC Annotation");

return mv;

}

 
 

 

}

 
 

 
 

 
 

3、在
SpringMVC 核心配置文件中进行配置

 
 

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

xmlns:mvc="http://www.springframework.org/schema/mvc"

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/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd">

 

 

<!-- 配置 HandlerMapping(可选,即
可以省略不配置) -->

<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

 

<!-- 配置 HandlerAdapter(可选,即
可以省略不配置) -->

<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

 

<!--

配置 Controller(必须,即
必须进行配置)

 

class 为自定义 Controller 类的完全限定名,这里通过 Controller

类中的 @RequestMapping 注解来设置访问路径(请求路径)

 

使用注解时,另一种配置 Controller 的方式:配置扫描包

<context:component-scan base-package="com.siwuxie095.controller" />

-->

<bean
class="com.siwuxie095.controller.HelloController"/>

 

<!-- 配置 ViewResolver(必须,即
必须进行配置) -->

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<!--

配置视图解析的前缀 prefix 和后缀 suffix:

(1)前缀:如果在 WebContent 目录下,则为 /,如果在 WEB-INF 目录下,则为 /WEB-INF/

(2)后缀:一般为 JSP 文件,所以为 .jsp

 

例如:prefix="/",suffix=".jsp",viewname="test",则:"/test.jsp"

-->

<property
name="prefix"
value="/"/>

<property
name="suffix"
value=".jsp"/>

</bean>

 
 

 

</beans>

 
 

 
 

 
 

4、编写一个
JSP 页面

 
 

hello.jsp:

 
 

<%@ page language="java"
contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!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=UTF-8">

<title>hello</title>

</head>

<body>

 
 

<h1>${msg}</h1>

 

</body>

</html>

 
 

 
 

 
 

5、访问路径

 
 

http://localhost:8080/工程名/hello.do

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

【made by siwuxie095】

SpringMVC入门(基于注解方式实现)的更多相关文章

  1. Shiro入门之二 --------基于注解方式的权限控制与Ehcache缓存

    一  基于注解方式的权限控制 首先, 在spring配置文件applicationContext.xml中配置自动代理和切面 <!-- 8配置自动代理 -->    <bean cl ...

  2. Elasticsearch-mapper 基于注解方式生成mapping(2.0以上)

    Elasticsearch生成mapping的方式上有多种方式,我们可以把mapping做成配置文件,也可以用spring-data-elasticsearch基于注解生成. 在基于注解生成这种方式上 ...

  3. Spring声明式事务管理(基于注解方式实现)

    ----------------------siwuxie095                                 Spring 声明式事务管理(基于注解方式实现)         以转 ...

  4. Spring boot 基于注解方式配置datasource

    Spring boot 基于注解方式配置datasource 编辑 ​ Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...

  5. 【Spring】SpringMVC之基于注解的实现SpringMVC+MySQL

    目录结构: contents structure [-] SprinigMVC是什么 SpringMVC工作原理 @Controller和@RequestMapping注解 @Controller注解 ...

  6. SpringMVC之基于注解的Controller

    参考博客:https://www.cnblogs.com/qq78292959/p/3760560.html Controller注解: 传统风格的Controller需要实现Controller接口 ...

  7. Spring中基于注解方式管理bean

    操作步骤 第一步:导入相关jar包 spring IoC的基本包 Spring支持注解的Jar包 第二步:创建Spring配置文件,ApplicationContext.xml 引入约束和开启注解扫描 ...

  8. SpringMVC基于注解方式的quartz

    项目框架: SpringMVC.MyBatis.JSP 1. 首先配置spring.xml文件 <?xml version="1.0" encoding="UTF- ...

  9. Hibernate基于注解方式的各种映射全面总结

    1. 使用Hibernate Annotation来做对象关系映射 1) 添加必须包: hibernate-jpa-2.0-api-1.0.0.Final.jar 2) 在实体类中添加JPA的标准注解 ...

随机推荐

  1. Bootstrap-CSS:网格系统

    ylbtech-Bootstrap-CSS:网格系统 1.返回顶部 1. Bootstrap 网格系统 本章节我们将讲解 Bootstrap 的网格系统(Grid System). Bootstrap ...

  2. [转]ASP.net Application 生命周期事件

    生命周期事件和 Global.asax 文件 在应用程序的生命周期期间,应用程序会引发可处理的事件并调用可重写的特定方法.若要处理应用程序事件或方法,可以在应用程序根目录中创建一个名为 Global. ...

  3. sersync基于rsync+inotify实现数据实时同步

    一.环境描述 需求:服务器A与服务器B为主备服务模式,需要保持文件一致性,现采用sersync基于rsync+inotify实现数据实时同步 主服务器A:192.168.1.23 从服务器B:192. ...

  4. hashmap引起死循环

    今天开发环境压测的时候出现cpu用满了情况,看线程堆栈,一堆线程都停留在org.apache.commons.collections4.map.AbstractHashedMap.put(Abstra ...

  5. 2018ICPC网络赛(焦作站)K题题解

    一.题目链接 https://nanti.jisuanke.com/t/31720 二.题意 给$N$种船只,第$i$种船的载重量是$V_i$,数量是$2^{C_i}-1$.接下来有$Q$次询问,每次 ...

  6. 安装HBase(0.9)数据库

    基本知识: 1.hbase是一种基于列存储的数据库,也就是说它的一列的数据是存储在一个文件里面的,而传统的数据库存储都是一个文件存储多个行,这些行有不同的列,这些列的数据类型 不同. 2.基于HDFS ...

  7. paycharm导入webdriver包报错:module 'selenium.webdriver' has no attribute 'Firefox'

    首先:试试看在cmd中试试输入from selenium import webdriver,看是否报错,看一看是不是pycharm的原因.经过确认,在dos窗口中输入导入包的命令并没有报错.最后我重现 ...

  8. uva-165-枚举

    题意:选取k种面额的邮票,总数是h,要求组合出来的连续数最大 枚举,网上看到一个更快的等价类划分,留着学等价类划分的思路 #include<stdio.h> #include<ios ...

  9. hive 下篇

    由于spark on hive 问题,导致无法插入数据,暂时使用spark进行hive操作 向分区表插入数据 hive> show partitions customers;OKpartitio ...

  10. 网络软工个人作业4——Alpha阶段个人总结

    1.个人总结 (1) 类型 具体技能和面试问题 现在的回答 毕业时找工作 语言 拿手的语言 Java 软件实现 有没有在别人的代码基础上进行改进,你是怎么读懂别人的代码,你采取什么方法不影响原来的功能 ...