概述
SpringMVC-参数绑定
默认类型
@RequestMapping("queryUser.do")
public ModelAndView queryUser(HttpServletRequest request,
HttpServletResponse response,
HttpSession session) {
ModelAndView mv = new ModelAndView();
//jsp的做法-获取地址栏中的参数的方法
String uid = request.getParameter("uid");
User user = userDao.queryUser(Integer.valueOf(uid));
mv.addObject("user",user);
mv.setViewName("order-view");
return mv;
}
简单类型
变量名相同
@RequestMapping("queryUser.do")
public ModelAndView queryUser(int uid,HttpServletRequest request,
HttpServletResponse response,
HttpSession session) {
ModelAndView mv = new ModelAndView();
//jsp的做法-获取地址栏中的参数的方法
//String uid = request.getParameter("uid");
User user = userDao.queryUser(Integer.valueOf(uid));
mv.addObject("user",user);
mv.setViewName("order-view");
return mv;
}
可以支持基本数据类型及其对应的包装类
注意点:该形式要求方法中的参数名必须和请求的参数名一致,该参数必须要有值
变量名不同
@RequestParamname:请求地址中的参数名
required:是否必须有值,默认为true
defaultValue:设置默认值
@RequestMapping("queryUser.do")
public ModelAndView queryUser(@RequestParam(name = "uid",required = false,defaultValue = "1") int id,
HttpServletRequest request,
HttpServletResponse response,
HttpSession session) {
ModelAndView mv = new ModelAndView();
//jsp的做法-获取地址栏中的参数的方法
//String uid = request.getParameter("uid");
User user = userDao.queryUser(id);
mv.addObject("user",user);
mv.setViewName("order-view");
return mv;
}
pojo类型
注意点:使用pojo类型自动映射要求表单中的字段名和实体类中的属性名保持一致
@RequestMapping("updateUser.do")
public ModelAndView updateUser(User user) {
ModelAndView mv = new ModelAndView();
int result = userDao.updateUser(user);
mv.addObject("result", result);
mv.setViewName("edit");
return mv;
}
<form class="layui-form" action="updateUser.do" method="post">
<div class="layui-form-item">
<label for="L_email" class="layui-form-label">
<span class="x-red">*</span>用户ID</label>
<div class="layui-input-inline">
<input type="text" id="uid" name="uid" value="${user.uid}" class="layui-input"></div>
</div>
<div class="layui-form-item">
<label for="L_username" class="layui-form-label">
<span class="x-red">*</span>用户姓名</label>
<div class="layui-input-inline">
<input type="text" id="uname" name="uname" value="${user.uname}" class="layui-input"></div>
</div>
<div class="layui-form-item">
<label for="L_pass" class="layui-form-label">
<span class="x-red">*</span>联系方式</label>
<div class="layui-input-inline">
<input type="text" id="phone" name="phone" value="${user.phone}" class="layui-input"></div>
</div>
<div class="layui-form-item">
<label for="L_repass" class="layui-form-label">
<span class="x-red">*</span>家庭住址</label>
<div class="layui-input-inline">
<input type="text" id="address" name="address" value="${user.address}" class="layui-input"></div>
</div>
<div class="layui-form-item">
<label for="L_repass" class="layui-form-label"></label>
<button class="layui-btn" lay-filter="add" lay-submit="">修改</button></div>
</form>
自定义类型
public class Custom {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
@RequestMapping("updateUserCustom.do")
public ModelAndView updateUserCustom(Custom custom) {
ModelAndView mv = new ModelAndView();
int result = userDao.updateUser(custom.getUser());
mv.addObject("result", result);
mv.setViewName("edit");
return mv;
}
<form class="layui-form" action="updateUserCustom.do" method="post">
<div class="layui-form-item">
<label for="L_email" class="layui-form-label">
<span class="x-red">*</span>用户ID</label>
<div class="layui-input-inline">
<input type="text" id="uid" name="user.uid" value="${user.uid}" class="layui-input"></div>
</div>
<div class="layui-form-item">
<label for="L_username" class="layui-form-label">
<span class="x-red">*</span>用户姓名</label>
<div class="layui-input-inline">
<input type="text" id="uname" name="user.uname" value="${user.uname}" class="layui-input"></div>
</div>
<div class="layui-form-item">
<label for="L_pass" class="layui-form-label">
<span class="x-red">*</span>联系方式</label>
<div class="layui-input-inline">
<input type="text" id="phone" name="user.phone" value="${user.phone}" class="layui-input"></div>
</div>
<div class="layui-form-item">
<label for="L_repass" class="layui-form-label">
<span class="x-red">*</span>家庭住址</label>
<div class="layui-input-inline">
<input type="text" id="address" name="user.address" value="${user.address}" class="layui-input"></div>
</div>
<div class="layui-form-item">
<label for="L_repass" class="layui-form-label"></label>
<button class="layui-btn" lay-filter="add" lay-submit="">修改</button></div>
</form>
SpringMVC-请求乱码处理(web.xml)
<!-- 字符编码过滤器 -->
<filter>
<filter-name>encoding</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>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
SpringMVC-注解的使用
@RequestMapping
@RequestMapping(value = “queryUsers.do”,method = {RequestMethod.POST,RequestMethod.GET})
@RequestMapping在类上使用时出现静态资源被过滤的问题(待处理)
SpringMVC-返回值处理
ModelAndView 数据+视图 很强大,万能 (没有分离)
String 视图(数据交给Model) 更符合MVC的模式(分离)
@RequestMapping(value = "/queryUsers.do",method = {RequestMethod.POST,RequestMethod.GET})
public String queryUsers(Model model) {
List<User> userList = userDao.queryUsers();
//request.setAttribute("data",userList)
model.addAttribute("data", userList);
return "order-list";
}
void ajax–json数据交互
最后
以上就是不安衬衫为你收集整理的Spring MVC第四讲(SpringMVC-参数绑定、SpringMVC-请求乱码处理、SpringMVC-注解的使用、SpringMVC-返回值处理)SpringMVC-参数绑定SpringMVC-请求乱码处理(web.xml)SpringMVC-注解的使用SpringMVC-返回值处理的全部内容,希望文章能够帮你解决Spring MVC第四讲(SpringMVC-参数绑定、SpringMVC-请求乱码处理、SpringMVC-注解的使用、SpringMVC-返回值处理)SpringMVC-参数绑定SpringMVC-请求乱码处理(web.xml)SpringMVC-注解的使用SpringMVC-返回值处理所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复