<body>
<a href="add">新增</a>
<a href="update">修改</a>
<a href="del">删除</a>
</body>
<body>
<a href="add">新增</a>
<a href="update">修改</a>
<a href="del">删除</a>
</body>

<?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") [email protected](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 * 那么我们只需要在 @[email protected]("/user") * 就相当于我们的namespace! [email protected]/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 false */ @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格式
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/12165.html