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 ...
随机推荐
- 【COGS1384】鱼儿仪仗队
[题目描述] Jzyz的池塘里有很多条鱼,鱼儿们现在决定组成一个仪仗队.现在备选的N(1 <= N <= 100,000)条鱼排成了一条直线,并且按照亲近关系排的队伍,鱼儿的顺序不能改变, ...
- 转型函数 Boolean()
布尔值有2种可能的值true和false;但 ECMAScript中所有类型的值都有与这两个 Boolean 值 等价的值.要将一个值转换为其对应的 Boolean 值,可以调用转型函数 Boolea ...
- JavaScript奇技淫巧44招
JavaScript是一个绝冠全球的编程语言,可用于Web开发.移动应用开发(PhoneGap.Appcelerator).服务器端开发(Node.js和Wakanda)等等.JavaScript还是 ...
- Unable to boot device in current state: Creating
安装完xcode6.1后,将其改名为Xcode6.1.app,再移动个位置,启动模拟器,问题来了: Unable to boot device in current state: Creating 解 ...
- An unspecified error occurred!
在我们生成证书的时候,有时候会遇到这个问题,明明刚从电脑的钥匙串申请的证书,却报错!遇到这个不用急.多试几次.不是你的生成的证书不管用,多半原因是因为你的网速太挫了!
- 百度统计js被劫持用来DDOS Github的JS注释
前几天在乌云看见了百度统计js被劫持用来DDOS Github,就想看看执行的核心JS是怎么样请求的. 就分析了下JS的执行,发现乌云解析的地方说错了. 文章里面说.大概功能就是关闭缓存后每隔2秒加载 ...
- 前端工程之模块化(来自百度FEX)
模块化 是一种处理复杂系统分解成为更好的可管理模块的方式,它可以把系统代码划分为一系列职责单一,高度解耦且可替换的模块,系统中某一部分的变化将如何影响其它部分就会变得显而易见,系统的可维护性更加简单易 ...
- 【HDOJ】4628 Pieces
最开始的想法是搜索,发现不对,后来发现数据量很小,可以状态压缩+DP. /* 4628 */ #include <cstdio> #include <cstring> #inc ...
- BZOJ 1002 [FJOI2007]轮状病毒
1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3106 Solved: 1724[Submit][Statu ...
- Makefile中的wildcard和patsubst
makefile 里的函数跟它的变量很相似——使用的时候,你用一个 $ 符号跟开括号,函数名,空格后跟一列由逗号分隔的参数,最后用关括号结束. 例如,在 GNU Make 里有一个叫 'wild ...