SpringMVC重定向传递参数
在SpringMVC的一个controller中要把参数传到页面,只要配置视图解析器,把参数添加到Model中,在页面用el表达式就可以取到。但是,这样使用的是forward方式,浏览器的地址栏是不变的,如果这时候浏览器F5刷新,就会造成表单重复提交的情况。所以,我们可以使用重定向的方式,改变浏览器的地址栏,防止表单因为刷新重复提交。
WEB-INF下web.xml文件:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
<display-name>Archetype Created Web Application</display-name> <servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
WEB-INF下springmvc-servlet文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com" />
<bean id="irViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/page/" />
<property name="suffix" value=".jsp" />
</beans>
*视图定位:作用是把视图约定在 webapp/page/*.jsp 这个位置
JSP文件:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%> <h1>${message}</h1> <form action="addProduct"> 产品名称 :<input type="text" name="name" value=""><br />
产品价格: <input type="text" name="price" value=""><br /> <input type="submit" value="增加商品">
</form>
Controller:
package com; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes; import pojo.Product; @Controller
public class IndexController{ @RequestMapping("/addProduct")
public String add(Product product, RedirectAttributes model){
model.addFlashAttribute("name", product.getName());
return "redirect:/showProduct";
} @RequestMapping("/showProduct")
public String show(@ModelAttribute("name") String name, Model model){
System.out.println(name);
model.addAttribute("name", name);
return "show";
}
}
*自动装箱:add方法里面准备一个Product参数,会自动获取表单提交数据
package pojo;
public class Product {
private int id;
private String name;
private float price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
出现的问题:
java.lang.IllegalStateException: Argument [RedirectAttributes] is of type Model or Map but is not assignable from the actual model. You may need to switch newer MVC infrastructure classes to use this argument.
解决:在springmvc-servlet.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
并加上这个标签:<mvc:annotation-driven />
SpringMVC重定向传递参数的更多相关文章
- springMVC controller间跳转 重定向 传递参数的方法
springMVC controller间跳转 重定向 传递参数的方法 spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参 ...
- 什么时候会进行 SpringMVC重定向保存参数(FlashMap)?
SpringMVC重定向保存参数(FlashMap):两种情况会保存参数: 1. 当前视图为RedirectView,也即是说当前请求为重定向请求. org.springframe ...
- JSP重定向传递参数
我一个JSP程序,要实现前台提交数据给后台处理后,后台jsp自动跳转到另一个jsp页面,这种方式也叫重定向,重定向的方法有多种,暂时我试过的并且能成功的有两个: 一种是用 response.sendR ...
- SpringMvc的传递参数方式 -- url / requestMapping
在使用spring的项目中,前台传递参数到后台是经常遇到的事, 我们必须熟练掌握一些常用的参数传递方式和注解的使用,废话少说,直接上正文. 1. @requestMapping: 类级别和方法级别的注 ...
- springMVC 重定向带参数
重定向时经常需要带上一定的标志位参数,当然,强大的springmvc提供了便利的操作. 只需要在方法参数中添加RedirectAttributes 或其子类即可! @RequestMapping(&q ...
- SpringMVC 页面传递参数到controller的五种方式
一共是五种传参方式: 一:直接将请求参数名作为Controller中方法的形参 public String login (String username,String password) : 解 ...
- Springmvc 前台传递参数到后台需要数据绑定
我们知道,当提交表单时,controller会把表单元素注入到command类里,但是系统注入的只能是基本类型,如int,char,String.但当我们在command类里需要复杂类型,如Integ ...
- springmvc重定向请求。
SpringMVC重定向传参数的实现(来自网友) 验证了我说的,从model层中拿来的数据,不管什么类型,都是通过隐含模型,中转,放入request中的.除非你特意把这些数据放到session域中. ...
- Springmvc 重定向参数传递方式
Springmvc 通过return "redirect:" 实现重定向 重定向的状态码301 302 301,302 都是HTTP状态的编码,都代表着某个URL发生了转移 ...
随机推荐
- -es6的部分语法
es6的语法 一 . let 和 var 的区别 : 1 . let 和 val 的区别 : ES6新增了let命令 , 用来声明变量,它的用法类似于 var (ES5), 但是所声明的变量,只在l ...
- 5.1 《锋利的jQuery》jQuery对表单的操作
获取焦点和失去焦点改变样式 改变文本框/滚动条高度 复选框应用 下拉框应用 表单验证 tip1: 注意使用<label>的for标签,对应input的id.(for 属性规定 label ...
- Eclipse for PHP Developers使用笔记
1 修改字体样式:Window-->Preference-->General-->Appearance-->Basic-->text font-->edit
- AOP和IOC的作用(转)
AOP和IOC的作用 转载▼ IOC:控制反转,是一种设计模式.一层含义是控制权的转移:由传统的在程序中控制依赖转移到由容器来控制:第二层是依赖注入:将相互依赖的对象分离,在spring配置文 ...
- PS 图像调整— — gain and bias
clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); Image=im ...
- poj 2559 最大矩形面积(单调栈)
题目:输入一个整数n,代表有n个 1(宽度) * h[i](高度)的矩形.接下来n个数依次给定一个矩形高度的高度h[i](i<=n). 求:在给定的依次排列的这堆矩形构成的图形里用一个矩形圈出 ...
- codevs 3314 魔法森林
传送门 3314 魔法森林 时间限制: 3 s 空间限制: 256000 KB 题目等级 : 大师 Master 题解 题目描述 Description 为了得到书法大家的真传,小E同学下定 ...
- Data Guard 异构平台支持手册
背景 最简单的DG 环境是同数据库版本,同操作系统平台. 但有时需要在异构平台上来部署DG 环境,比如异构下使用DG 来进行数据迁移,从而降低停机时间和风险等等. 1. 查看主备库的Platform ...
- YPBPR_PC下图像有毛刺或者水纹干扰的处理办法
VGA下一般是不做任何处理的,当然也不排除个别情况下,客户强烈要求,会打开的现象. (1)做一下auto color和auto adjust (2)调节一下ADC,如果是mstar的方案,将对比机的的 ...
- struts2的使用知识点
最开始学习java的时候学习过struts,但是对配置和struts的理解深度不够,现在工作虽然再用,但是自己搭建环境和使用心得始终很零散,所以现在决定重新理一遍,有条理的学习一下struts. 至于 ...