SpringMVC重定向传递参数
2024-08-29 10:21:34
在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 />
最新文章
- 《Entity Framework 6 Recipes》翻译系列 (5) -----第二章 实体数据建模基础之有载荷和无载荷的多对多关系建模
- css中background背景属性概
- vim - Convert between hex and decimal
- 【Go语言】学习资料
- 通过SEP禁用USB
- Android 上传图片到 Asp.Net 服务器的问题
- 很值得学习的java 画图板源码
- [OC Foundation框架 - 17] copy语法
- 【leetcode】Merge Sorted Array(合并两个有序数组到其中一个数组中)
- 蓝桥杯-逆波兰表达式-java
- JSONP原理解析
- spring boot整合spring5-webflux从0开始的实战及源码解析
- vue实现实时监听文本框内容的变化(最后一种为原生js)
- 微信机器人 返现机器人 pc版本 移动版本 java开发 小范省钱
- IMPLEMENTATION - Entity Framework Anti Pattern - High Performance EF
- 网页一键加入QQ群
- opencv error: undefined reference to `png_set_expand_gray_1_2_4_to_8@PNG16_0&#39;
- Android中关于使用空格对齐文字
- 001_nginx常用参数查询
- C#之数据类型学习