Spring MVC 基础注解之@RequestMapping、@Controller、(二)
我现在学的是spring4.2
今天主要学习了Spring MVC注解
引入注解可以减少我们的代码量,优化我们的代码。
@Controller:用于标识是处理器类;
@RequestMapping:请求到处理器功能方法的映射规则;
还是以示例来解释说明
1 创建springAnnotation02项目,导入jar包。
这里的web.xml
- <?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
- <display-name>springmvc01</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>springAnnotation</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <!-- 默认情况下:DispatcherServlet会寻找WEB-INF下,命名规范为[servlet-name]-servlet.xml文件。如:在上例中,它就会找/WEB-INF/spring-servlet.xml
- 如果需要修改,需要在web.xml中的<servlet>标记中增加 <init-param>。。。 </init-param>:-->
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/classes/springAnnotation-servlet.xml</param-value>
- </init-param>
- <!-- load-on-startup:表示启动容器时初始化该Servlet; -->
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>springAnnotation</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <!-- 动态设置项目的运行路径 -->
- <context-param>
- <param-name>webAppRootKey</param-name>
- <param-value>spring.root</param-value>
- </context-param>
- <!-- 加载LOG4J -->
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>/WEB-INF/log4j.xml</param-value>
- </context-param>
- <context-param>
- <param-name>log4jRefreshInterval</param-name>
- <param-value>60000</param-value>
- </context-param>
- <!-- spring Web MVC框架提供了org.springframework.web.filter.CharacterEncodingFilter
- 用于解决POST方式造成的中文乱码问题 -->
- <filter>
- <filter-name>encodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- 启动log4j日志管理 -->
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
- </web-app>
- <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"
- xsi:schemaLocation=
- "http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
修改过后的springAnnotation-servlet.xml。
- <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-3.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
使用Spring mvc注解时一个完整的springAnnotation-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-3.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
- <!--开启注解 -->
- <mvc:annotation-driven />
- <!--启用自动扫描 -->
- <context:component-scan base-package="com.cy.springannotation.controller" />
- <!--配置视图解析器 -->
- <!-- ViewResolver 视图解析器 用于将返回的ModelAndView对象进行分离
- InternalResourceViewResolver:用于支持Servlet、JSP视图解析;
- viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,classpath中必须包含jstl的相关jar包;
- prefix和suffix:查找视图页面的前缀和后缀(前缀[逻辑视图名]后缀),
- 比如传进来的逻辑视图名为WEB-INF/jsp/hello,则该该jsp视图页面应该存放在“WEB-INF/jsp/hello.jsp”; -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
- <property name="prefix" value="/"></property> <!-- 我这里的视图直接放在WebRoot下的 -->
- <property name="suffix" value=".jsp"></property>
- </bean>
- <!-- 为何不配置HandlerMapping?因为基于注解时,会自动使用DefaultAnnotationHandlerMapping -->
- </beans>
接下来,我们完成一个最简单的通过控制实现页面间的跳转。
一个有提交的页面
- <%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>登录页面</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- <form action="login.do" method="post">
- <table>
- <tr>
- <td>用户名:</td>
- <td><input type="text" name="username"/></td>
- </tr>
- <tr>
- <td>班级</td>
- <td><input type="text" name="clazz"/></td>
- </tr>
- <tr>
- <td colspan="2"> <input type="submit" value="提交"/> </td>
- </tr>
- </table>
- </form>
- </body>
- </html>
控制器
- package com.cy.springannotation.controller;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- @Controller // @Controller告知Spring容器这是一个控制器组件
- public class LoginController {
- @RequestMapping("/login.do") // @RequestMapping告知该方法是针对/login.do请求的处理方法
- public String login(String username){
- System.out.println(username);
- return "index"; // 返回的字符串被当做ViewName
- }
- }
以上的代码就可以进行一个简单的页面跳转。
接下来进一步了解@RequestMapping的特性
- @Controller
- @RequestMapping("/test")
- public class login.doController {
- @RequestMapping("/login.do")
- public String login(){
- return "index";
- }
- }
- //此时代表请求该方法的路径是:/test/login.spring
@RequestMapping(value=“/users/**”) 匹配“/users/abc/abc”;
@RequestMapping(value="/product?") 匹配“/product1”或“/producta”,但不匹配“/product”或“/productaa”;
@RequestMapping(value="/product*") 匹配“/productabc”或“/product”,但不匹配“/productabc/abc”;
@RequestMapping(value="/product/*") 匹配“/product/abc”,但不匹配“/productabc”;
@RequestMapping(value="/login.do",params="flag") 代表请求中必须要有名为flag的提交项
@RequestMapping(value="/login.do",params="!flag") 代表请求中不能有名为flag的提交项
@RequestMapping(value="/login.do",params="flag=hello") 代表请求中必须有名为flag的提交项,且值为hello
@RequestMapping(value="/login.do",params="flag!=hello") 代表请求中如果有名为flag的提交项,其值不能为hello
@RequestMapping(value="/login.do",params={"flag1","flag2=hello"})代表请求中必须有名为flag1的提交项,同时必须有名为flag2的提交项,且flag2的值必须为hello
@RequestMapping(value="/login.do",method=RequestMethod.POST)
@RequestMapping(value="/login.do",method=RequestMethod.GET)
@RequestMapping(value="/login.do", method= {RequestMethod.POST, RequestMethod.GET}"
接下来是:请求处理方法可接收参数
Spring MVC 基础注解之@RequestMapping、@Controller、(二)的更多相关文章
- Spring MVC学习总结(2)——Spring MVC常用注解说明
使用Spring MVC的注解及其用法和其它相关知识来实现控制器功能. 02 之前在使用Struts2实现MVC的注解时,是借助struts2-convention这个插件,如今我们使 ...
- spring mvc 基于注解的使用总结
本文转自http://blog.csdn.net/lufeng20/article/details/7598801 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sprin ...
- Spring MVC 基础
Spring MVC 基础 1.Web MVC基础 MVC的本质是表现层模式,我们以视图模型为中心,将视图和控制器分离出来.就如同分层模式一样,我们以业务逻辑为中心,把表现层和数据访问层代码分离出来是 ...
- 基于spring mvc的注解DEMO完整例子
弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件.本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mv ...
- spring mvc 基于注解 配置默认 handlermapping
spring mvc 是类似于 Struts 的框架.他们都有一个最主要的功能就是URL路由.URL路由能将请求与响应请求处理逻辑的类(在Struts中即是action,在spring mvc 中即是 ...
- Spring MVC : Java模板引擎 Thymeleaf (二)
本文原计划直接介绍Thymeleaf的视图解析,但考虑到学习的方便,决定先构建一个spring-mvc. 以下的全部过程仅仅要一个记事本和JDK就够了. 第一步,使用maven构建一个web app. ...
- Spring MVC——基础(简介,使用,地址映射)
Spring MVC简介 重点Spring MVC的处理流程 Spring MVC特点 Spring MVC的使用 将相应的JAR包导入lib文件下 配置相关webxml 配置servlet-mvcx ...
- Spring的基础注解
Spring的基础注解 1.注解的概述 注解是为了便于程序的调试而用于代替配置文件的一种程序语法,与配置文件具有互换性.通常基于注解编程的程序更加简洁. (注:使用Spring注解必须导入aop包) ...
- spring mvc: 多动作控制器(Controller下面实现多个访问的方法)MultiActionController / BeanNameUrlHandlerMapping
spring mvc: 多动作控制器(Controller下面实现多个访问的方法) 比如我的控制器是UserController.java,下面有home, add, remove等多个方法 访问地址 ...
随机推荐
- 棋盘问题 分类: 搜索 POJ 2015-08-09 13:02 4人阅读 评论(0) 收藏
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28474 Accepted: 14084 Description 在一 ...
- Linux文件描述符与打开文件之间的区别(转载)
转载请说明出处:http://blog.csdn.net/cywosp/article/details/38965239 1. 概述 在Linux系统中一切皆可以看成是文件,文件又可分为: ...
- google prettify 代码高亮显示
引入js和css文件 下载地址 http://files.cnblogs.com/jaday/prettify.zip js文件代码 !function(){var q=null;window.PR_ ...
- 取模(mod)
取模(mod) [题目描述] 有一个整数a和n个整数b_1, …, b_n.在这些数中选出若干个数并重新排列,得到c_1,…, c_r.我们想保证a mod c_1 mod c_2 mod … mod ...
- 土豪聪要请客(stol)
土豪聪要请客(stol) 众所周知,聪哥(ndsf)是个土豪,不过你们不知道的是他的MZ和他的RMB一样滴多…… 某天土豪聪又赚了10^10000e的RMB,他比较开心,于是准备请客.他在自己在XX星 ...
- Spark系列之二——一个高效的分布式计算系统
1.什么是Spark? Spark是UC Berkeley AMP lab所开源的类Hadoop MapReduce的通用的并行计算框架,Spark基于map reduce算法实现的分布式计算,拥有H ...
- java中的构造函数
在c++中就学习了构造函数,今天学习java又碰到了构造函数,重新写一篇博客来理解一下 其实直接听这个词并不能理解这是什么,但其实看了它的作用,就很好理解了 当创建一个对象时,往往需要做一些初始化工作 ...
- 1-NPM
什么是NPM NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题. 常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从N ...
- java基础 (java工程师入门应该了解的)
1)建立Java开发环境 2)理解Java特性(features) 3)理解Java虚拟机软件的功能 4)描述垃圾回收概念 5)枚举实现Java代码安全方式 6)编码 编译 运行 JAav 应用程序 ...
- Win7 winsock 注册表文件
http://files.cnblogs.com/xsmhero/Winsock_Win7x64.rar