在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 />

最新文章

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

热门文章

  1. Android 进程增加存活率
  2. smokeping高级配置
  3. 0-mybatis目录
  4. Maven项目中使用JUnit进行单元测试
  5. django 模板中通过变量替代key取字典内容
  6. POJ3728The merchant (倍增)(LCA)(DP)(经典)(||并查集压缩路径?)
  7. WPF开发学习笔记(转)
  8. C#如何把XSD中HexBinary类型序列化uint类型
  9. CHAKRA3 UART2
  10. 系统启动挂载根文件系统时Kernel panic