一个基础的springmvc的页面访问及提交数据的小知识

507 查看

今天跟小伙伴们分享一个小知识点,是使用springmvc框架提供的表单来提交数据到后台并将结果显示在结果页面。我尽可能的用简单的表述和简洁的代码表达,这对我也是一种锻炼。有些关联的小知识点我以后会慢慢写。如果新小白还是看不明白,也欢迎提问,我尽可能的解释明白。需求是添加鸭子,我们添加一只经典小黄鸭。

大致的过程是下面这幅图:

访问的表单页面:addduck.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%--
  Created by IntelliJ IDEA.
  User: deer
  Date: 15/7/4
  Time: 下午4:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
    <%--
    <form:form></form:form>这是springmvc表单的写法,上面会引入它的标签库
    action与method与html的表单一样
    modelAttribute="duck"用于接收Controller传过来的默认值就是addGood()方法里面model.addAttribute("duck", duck);
    这里会将传过来的duck当成默认值显示出来

    点击提交按钮后会将值传给Controller的public String result(ModelMap model, @RequestParam String name, @RequestParam String color)方法
    --%>
    <form:form action="result" method="post" modelAttribute="duck">
        名字:<form:input path="name"></form:input><br>
        颜色:<form:input path="color"></form:input><br>
        <input type="submit" value="提交"/>
    </form:form>
</body>
</html>

Duck实例类:Duck.java

package com.springapp.entity;

public class Duck {
    //小鸭子的名称
    private String name;
    //小鸭子的颜色
    private String color;
    //get与set方法
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
}

HelloController控制层代码:HelloController.java

package com.springapp.mvc;
import com.springapp.entity.Duck;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/")
public class HelloController {
    //addduck.jsp页面的表单将将数据提交给result,那么就这这个方法来接收了
    //method = RequestMethod.POST对应的是addduck.jsp页面表单的提交方式
    //@RequestParam String name, @RequestParam String color是接收form表单传递过来的参数
    @RequestMapping(value = "/result", method = RequestMethod.POST)
    public String result(ModelMap model, @RequestParam String name, @RequestParam String color){
        //同样放进model中,用于在result.jsp页面获取并显示
        model.addAttribute("name", name);
        model.addAttribute("color", color);
        //返回到result.jsp页面
        return "result";
    }

    //下面的注解参数:
    //value = "/addduck",是说浏览器的访问地址这里就是http://localhost:8080/addduck
    //method = RequestMethod.GET是说接收jsp页面访问的方式,通过浏览器直接访问那就是get方式了
    @RequestMapping(value = "/addduck", method = RequestMethod.GET)
    public String addDuck(ModelMap model){
        //实例化一个Duck,这里没有使用spring依赖注入
        Duck duck = new Duck();
        //设置值
        duck.setName("经典小黄鸭");
        duck.setColor("黄色");
        //将duck放进model中用于在jsp页面获取用
        model.addAttribute("duck", duck);
        //返回addduck.jsp页面
        return "addduck";
    }
}

最后呈现结果的页面:result.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
    <%--
    这里使用springmvc表达式将result方法传过来的值显示出来
    --%>
    名字:${name}<br>
    颜色:${color}
</body>
</html>

最后为了避免页面出现中文乱码,在web.xml中添加编码配置,建议加在上面

<filter>
    <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>