SpringMVC05使用注解的方式
<body>
<a href="add">新增</a>
<a href="update">修改</a>
<a href="del">删除</a>
</body>
在webroot下修改index.jsp页面
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 配置需要扫描的包 -->
<context:component-scan base-package="cn.bdqn.controller"/> </beans>
修改springmvc的核心xml文件
@Controller
public class MyController { /**
* @RequestMapping("/add") 等于@RequestMapping(value="/add")
* 如果只有value属性值,可以省略
* 代表用户访问的url
* 新增
*/
@RequestMapping("/add")
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) {
return new ModelAndView("/WEB-INF/jsp/success.jsp", "result",
"新增的方法......");
} // 修改
@RequestMapping("/update")
public ModelAndView update(HttpServletRequest request,
HttpServletResponse response) {
return new ModelAndView("/WEB-INF/jsp/success.jsp", "result",
"修改的方法......");
} // 删除
@RequestMapping("/del")
public ModelAndView del(HttpServletRequest request,
HttpServletResponse response) {
return new ModelAndView("/WEB-INF/jsp/success.jsp", "result",
"删除的方法......");
}
}
MyController
=======================请求参数的传递========================
<body>
<%-- 01.使用注解实现页面之间的跳转 --%>
<a href="user/add">增加</a>
<a href="user/update">修改</a>
<a href="user/del">删除</a>
<%-- 02.请求中 携带参数 --%>
<form action="user/addUser" method="post">
<input type="text" name="userName"/>
<input type="password" name="pwd"/>
<input type="submit" value="新增"/>
</form> </body>
package cn.bdqn.controller; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; /**
* @Controller: 就是配置我们的控制器
*
* 既然 每个方法上面都有 user
* 那么我们只需要在 @Controller中新增@RequestMapping("/user")
* 就相当于我们的namespace! 每个方法的@RequestMapping中都前缀加上了/user
*/
@Controller
@RequestMapping("/user")
public class UserController { /**
* @RequestMapping("/add") :用户访问的url /add 等同于 @RequestMapping(value="/add")
* 如果只有一个value属性,则可以省略value,直接书写value 的属性值!
* 如果有多个属性的时候,切记,value不能省略!
*/
@RequestMapping("/add")
public String add(){
System.out.println("add");
return "success";
} @RequestMapping("/update")
public String update(){
System.out.println("update");
return "success";
}
/**
* 通配符的使用
* *:只能是一级目录
* user/sasas/del true
* user/sasas/sas/del false
* **:可以没有 也可以多个目录! 0-N
* user/sasas/del true
* user/sasas/sas/del true
*/
@RequestMapping("/**/del")
public String del(){
System.out.println("del");
return "success";
} /**
* 请求中携带参数
* @RequestMapping(value={"/addUser","/haha","heihei"})
* @RequestMapping({"/addUser","/haha","heihei"})
* 多个请求 都会匹配我们当前的方法 @RequestMapping(value={"/addUser","/haha","heihei"})
public String addUser(HttpServletRequest request,HttpServletResponse response){
System.out.println(request.getParameter("userName"));
System.out.println(request.getParameter("pwd"));
return "success";
}
*/ /**
* params={"userName"}:请求过来的时候,参数必须有userName
* params={"!userName"}:请求过来的时候,参数必须没有userName
* params={"userName=admin"}:请求过来的时候,参数userName的值必须是admin
*/
@RequestMapping(value="/addUser",params={"userName=admin"})
public String addUser(HttpServletRequest request,HttpServletResponse response){
System.out.println(request.getParameter("userName"));
System.out.println(request.getParameter("pwd"));
return "success";
} }
======================获取前台的数据 并解决乱码问题=======================
在index页面新增
<%-- 03.请求中 携带参数 后台接收的时候出现乱码 并且返回
get方式乱码: conf文件夹下面的server.xml文件中配置 URIEncoding=UTF-8
post:使用filter ! spring mvc给你写好了!我们需要配置即可!
--%>
<form action="user/addUser2" method="post">
<input type="text" name="userName"/>
<input type="password" name="pwd"/>
<input type="submit" value="新增"/>
</form>
在controller中新增
@RequestMapping(value="/addUser2")
public String addUser2(HttpServletRequest request,HttpServletResponse response){
System.out.println(request.getParameter("userName"));
System.out.println(request.getParameter("pwd"));
return "success";
}
发现乱码问题,在web.xml文件中新增
<!--解决post请求乱码的问题 -->
<filter>
<filter-name>charset</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!--保证后台的encoding 不为空 而且还设置了编码格式 -->
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<!--
底层代码
this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)
request.getCharacterEncoding():什么时候不为null
01.前台设置了contentType="text/html; charset=ISO-8859-1"
02.设置request.setCharacterEncoding()
我们必须强制的让forceEncoding=true
-->
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>charset</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Request.getCharacterEncoding()
有两种情况不会为null
第一种是在jsp页面设置了contentType="text/html; charset=ISO-8859-1"
还有一种就是setCharacterEncoding了
一旦前台设置了contentType="text/html; charset=ISO-8859-1"
那么就会默认执行前台设置的编码! 为了按照我们定义的编码格式
所以 需要在web.xml中也要摄者forceEncoding=true
这样就能保证无论前台有没有设置 都会执行我们设置的utf-8格式
SpringMVC05使用注解的方式的更多相关文章
- java web学习总结(二十一) -------------------模拟Servlet3.0使用注解的方式配置Servlet
一.Servlet的传统配置方式 在JavaWeb开发中, 每次编写一个Servlet都需要在web.xml文件中进行配置,如下所示: 1 <servlet> 2 <servlet- ...
- Mybatis框架基于注解的方式,实对数据现增删改查
编写Mybatis代码,与spring不一样,不需要导入插件,只需导入架包即可: 在lib下 导入mybatis架包:mybatis-3.1.1.jarmysql驱动架包:mysql-connecto ...
- JavaWeb学习总结(四十八)——模拟Servlet3.0使用注解的方式配置Servlet
一.Servlet的传统配置方式 在JavaWeb开发中, 每次编写一个Servlet都需要在web.xml文件中进行配置,如下所示: 1 <servlet> 2 <servlet- ...
- Spring+AOP+Log4j 用注解的方式记录指定某个方法的日志
一.spring aop execution表达式说明 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义 ...
- springMvc的注解注入方式
springMvc的注解注入方式 最近在看springMvc的源码,看到了该框架的注入注解的部分觉的有点吃力,可能还是对注解的方面的知识还认识的不够深刻,所以特意去学习注解方面的知识.由于本人也是抱着 ...
- spring schedule定时任务(一):注解的方式
我所知道的java定时任务的几种常用方式: 1.spring schedule注解的方式: 2.spring schedule配置文件的方式: 3.java类继承TimerTask: 第一种方式的实现 ...
- 用注解的方式实现Mybatis插入数据时返回自增的主键Id
一.背景 我们在数据库表设计的时候,一般都会在表中设计一个自增的id作为表的主键.这个id也会关联到其它表的外键. 这就要求往表中插入数据时能返回表的自增id,用这个ID去给关联表的字段赋值.下面讲一 ...
- Spring学习笔记3——使用注解的方式完成注入对象中的效果
第一步:修改applicationContext.xml 添加<context:annotation-config/>表示告诉Spring要用注解的方式进行配置 <?xml vers ...
- mybatis 多个接口参数的注解使用方式(@Param)
目录 1 简介 1.1 单参数 1.2 多参数 2 多个接口参数的两种使用方式 2.1 Map 方法(不推荐) 2.1.1 创建接口方法 2.1.2 配置对应的SQL 2.1.3 调用 2.2 @Pa ...
随机推荐
- Xshell下漂亮的开发环境配置
今天折腾了一天Xshell配置Linux命令行开发环境. 总结几点: 1.Xshell配色方案,这是我自己调的个人使用版,网上比较好的版本有Solarized Dark,可以下载到. [ColorFo ...
- 下拉菜单选择(jQuery实现)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- yii2源码学习笔记(二)
yii\base\Object代码详解 <?php /** * @link http://www.yiiframework.com/ * @copyright Copyright (c) 200 ...
- jquery easy ui 学习 (4) window 打开之后 限制操纵后面元素属性
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- jquery ajax(4).getjson()
.getJSON()实例 .each()实例 $(function(){ $('#send').click(function() { $.getJSON('test.json', function(d ...
- [转载]在 Windows 10 中, 如何卸载和重新安装 OneNote App
在 Windows 10 中, 如何卸载和重新安装 OneNote App 15/8/2015 使用 PowerShell 命令卸载 OneNote App 开始菜单 -> 输入 "P ...
- STM32f103------按键处理
(1)按键去抖 /******************************************函数名称:Key_Scan(GPIO_TypeDef*GPIOx,u16 GPIO_pin)*描 ...
- C语言itoa函数和atoi 函数
C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转 换为字符串的一个例子: # include <stdio.h> ...
- 从Profile中窥探Unity的内存管理
刨根问底U3D---从Profile中窥探Unity的内存管理 这篇文章包含哪些内容 这篇文章从Unity的Profile组件入手,来探讨一下Unity在开发环境和正式环境中的内存使用发面的一些区别, ...
- keil优化等级设置
附表:Keil C51中的优化级别及优化作用 级别说明 0 常数合并:编译器预先计算结果,尽可能用常数代替表达式.包括运行地址计算. 优化简单访问:编译器优化访问8051系统的内部数据和位地址. 跳转 ...