SpringMVC入门(基于注解方式实现)
---------------------siwuxie095
SpringMVC 入门(基于注解方式实现)
SpringMVC 通过一套 MVC 注解,让 POJO 无需实现任何接口即可成
为处理请求的控制器,具体实现如下:
1、在部署描述文件中进行配置
web.xml:
|
<?xml <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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 <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
<!-- 配置 HandlerAdapter(可选,即 <bean
<!-- 配置 Controller(必须,即
class 为自定义 Controller 类的完全限定名,这里通过 Controller 类中的 @RequestMapping 注解来设置访问路径(请求路径)
使用注解时,另一种配置 Controller 的方式:配置扫描包 <context:component-scan base-package="com.siwuxie095.controller" /> --> <bean
<!-- 配置 ViewResolver(必须,即 <bean <!-- 配置视图解析的前缀 prefix 和后缀 suffix: (1)前缀:如果在 WebContent 目录下,则为 /,如果在 WEB-INF 目录下,则为 /WEB-INF/ (2)后缀:一般为 JSP 文件,所以为 .jsp
例如:prefix="/",suffix=".jsp",viewname="test",则:"/test.jsp" --> <property <property </bean>
</beans> |
4、编写一个
JSP 页面
hello.jsp:
|
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" <html> <head> <meta <title>hello</title> </head> <body> <h1>${msg}</h1>
</body> </html> |
5、访问路径
http://localhost:8080/工程名/hello.do
【made by siwuxie095】
SpringMVC入门(基于注解方式实现)的更多相关文章
- Shiro入门之二 --------基于注解方式的权限控制与Ehcache缓存
一 基于注解方式的权限控制 首先, 在spring配置文件applicationContext.xml中配置自动代理和切面 <!-- 8配置自动代理 --> <bean cl ...
- Elasticsearch-mapper 基于注解方式生成mapping(2.0以上)
Elasticsearch生成mapping的方式上有多种方式,我们可以把mapping做成配置文件,也可以用spring-data-elasticsearch基于注解生成. 在基于注解生成这种方式上 ...
- Spring声明式事务管理(基于注解方式实现)
----------------------siwuxie095 Spring 声明式事务管理(基于注解方式实现) 以转 ...
- Spring boot 基于注解方式配置datasource
Spring boot 基于注解方式配置datasource 编辑 Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...
- 【Spring】SpringMVC之基于注解的实现SpringMVC+MySQL
目录结构: contents structure [-] SprinigMVC是什么 SpringMVC工作原理 @Controller和@RequestMapping注解 @Controller注解 ...
- SpringMVC之基于注解的Controller
参考博客:https://www.cnblogs.com/qq78292959/p/3760560.html Controller注解: 传统风格的Controller需要实现Controller接口 ...
- Spring中基于注解方式管理bean
操作步骤 第一步:导入相关jar包 spring IoC的基本包 Spring支持注解的Jar包 第二步:创建Spring配置文件,ApplicationContext.xml 引入约束和开启注解扫描 ...
- SpringMVC基于注解方式的quartz
项目框架: SpringMVC.MyBatis.JSP 1. 首先配置spring.xml文件 <?xml version="1.0" encoding="UTF- ...
- Hibernate基于注解方式的各种映射全面总结
1. 使用Hibernate Annotation来做对象关系映射 1) 添加必须包: hibernate-jpa-2.0-api-1.0.0.Final.jar 2) 在实体类中添加JPA的标准注解 ...
随机推荐
- centos7开机界面出现多个选项
第一个选项正常启动,第二个选项急救模式启动(系统出项问题不能正常启动时使用并修复系统) 在CentOS更新后,并不会自动删除旧内核.所以在启动选项中会有多个内核选项,可以手动使用以下命令删除多余的内核 ...
- java study1
java安装 java优势-跨平台:一次编写,到处运行. jdk开发工具包,提供了开发人员需要的开发工具.jdk中包含了jre jre java的运行环境,负责程序的运行,jre中,包含程序运行时需要 ...
- [UE4]Selector和Sequence的区别
Selector和Sequence子节点都是返回true才会执行下一个子节点. Sequence是从左到右依次执行,左边节点如果返回false,则不会执行右边的节点 Selector会同步执行所有子节 ...
- google浏览器清除缓存
google浏览器设置不缓存的方法 摘要:我们在做web开发的时候特别是在调试js时,会经常使用的google浏览器,这个时候就要我们修改过的代码可能不能生效.这是因为我们的浏览器默认是有缓存的,但是 ...
- MySQL ALTER讲解
当我们需要修改数据表名或者修改数据表字段时,就需要使用到MySQL ALTER命令. 开始本章教程前让我们先创建一张表,表名为:testalter_tbl. root@host# mysql -u r ...
- Jenkins配置HTML报告(Windows环境)
1.首先安装插件HTML Publisher,点击直接安装 2.在任务中配置,构建后操作,添加Publish HTML reports 3.添加完成后,新增一项 4.HTML directory to ...
- BOM及DOM及事件
知识内容: 1.BOM介绍 2.DOM操作 3.事件 参考:http://www.cnblogs.com/liwenzhou/p/8011504.html 入门代码(DOM基本操作与事件): < ...
- ES6进一步整理
内容: 1.变量及赋值 2.函数 3.数组及json 4.字符串 5.面向对象 6.Promise 7.generator 8.模块 1.变量及赋值 (1)ES5变量定义 var: 可以重复定 ...
- sparkcontext 和 sparkconf
http://spark.apache.org/docs/latest/rdd-programming-guide.html The first thing a Spark program must ...
- Spring-boot+Mybatis+Maven+MySql搭建实例
转自:https://www.jianshu.com/p/95fb7be049ae 最近读了spring-boot开发手册,spring-boot相比于spring-mvc封装了很多常用的依赖,并且内 ...