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 ...
随机推荐
- TalkingData Cocos2dx在android平台使用总结
前言:最近发现很多朋友在使用TalkingData游戏版本Cocos2dx SDK使用过程中会出现的一些问题,今天来做一下总结,希望对您有所帮助: 首先非常感谢您使用TalkingData游戏统计平台 ...
- JQUERY1.9学习笔记 之内容过滤器(三) has选择器
描述:选择至少包含一个元素,匹配指定的标签的标签.jQuery( ":has(selector)" ) 例:给所有的div添加一个类"test",在他们中有一个 ...
- mysql hash索引优化
创建表 CREATE TABLE `t1` ( `id` int(11) NOT NULL AUTO_INCREMENT, `msg` varchar(20) NOT NULL DEFAULT ' ...
- php中抽象类和接口的特点、区别和选择
一.特点: 1.抽象类特点 (1) 用 abstract 来修饰一个类,那么这个类就是抽象类:抽象类绝对不能被实例化,即$abc = new 抽象类名();会报错. (2) 用abstract 来修饰 ...
- srpm包的编译方式
基本说明:后缀仅为rpm的包如xxxxx.rpm称作为二进制包 ------ 可以直接安装到架构匹配的系统上; 后缀为src.rpm的包如webkitgtk-2.4.7-1.fc21.src.rpm称 ...
- JVM笔记-temp
jvm源码分析之堆外内存完全解读 http://lovestblog.cn/blog/2015/05/12/direct-buffer/
- Fedora 19+ 启动顺序调整
首先找到Windows 8的menuentry cat /boot/grub2/grub.cfg | grep Windows 设置Windows 作为默认的启动项 grub2-set-default ...
- PostBack与IsPostBack区别
这涉及到aspx的页面回传机制的基础知识 postback是回传 即页面在首次加载后向服务器提交数据,然后服务器把处理好的数据传递到客户端并显示出来,就叫postback, ispostback只是一 ...
- hdu 5124 lines
http://acm.hdu.edu.cn/showproblem.php?pid=5124 题意:给你n条线段,然后找出一个被最多条线段覆盖的点,输出覆盖这个点的线段的条数. 思路:可以把一条线段分 ...
- c/c++关于内存分配的知识(非常详细的比较,且VirtualAlloc分配内直接在进程的地址空间中保留一快内存)
一个由c/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中的栈. 2.堆区(heap) — 一 ...