SpringMVC -- 梗概--源码--壹--收参

Spring MVC 参数处理
本文介绍了一个使用Spring MVC框架实现的示例项目,演示了如何通过不同的方式进行参数接收,包括基本类型参数、实体类参数、数组及集合类型的参数接收,并展示了如何解决参数乱码问题。

附:实体类

Class : User

package com.c61.entity;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;

import com.alibaba.fastjson.annotation.JSONField;

public class User {
    private Integer id;
    private String name;
    //@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern="yyyy-MM-dd")//定制在接收请求参数时的日期格式
    @JSONField(format="yyyy-MM-dd")//作用在java序列化成json时
    private Date birth;
    private String dateStr;
    
    public String getDateStr() {
        return dateStr;
    }
    public void setDateStr(String dateStr) {
        this.dateStr = dateStr;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getBirth() {
        return birth;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        this.dateStr=format.format(birth);
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", birth=" + birth + "]";
    }
    public User(){}
    public User(Integer id, String name, Date birth) {
        super();
        this.id = id;
        this.name = name;
        this.birth = birth;
    }
    
}

Class : ValueObject

package com.c61.entity;

import java.util.List;

public class ValueObject{
    private List<User> users;
    private String[] ids;
    public List<User> getUsers() {
        return users;
    }
    public void setUsers(List<User> users) {
        this.users = users;
    }
    public String[] getIds() {
        return ids;
    }
    public void setIds(String[] ids) {
        this.ids = ids;
    }
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 前端控制器 
         /=默认的url-pattern
         /a/b/c  /a
         
         /a/d/c
         /a/d
         /a
         /
         *注意:此控制器默认加载/WEB-INF下的xxx-servlet.xml文件
                        :其中xxx等于【DispatcherServlet的配置名】
  -->
  <servlet>
      <servlet-name>mvc61</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:mvc62.xml</param-value>
      </init-param>
      <!-- 随项目启动而启动 -->
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>mvc61</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <!-- 专治Post请求参数乱码 -->
  <filter>
      <filter-name>encoding61</filter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      <!-- 将请求的编码方式设置为utf-8 -->
      <init-param>
          <param-name>encoding</param-name>
          <param-value>utf-8</param-value>
      </init-param>
  </filter>
  <filter-mapping>
      <filter-name>encoding61</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

2.配置控制器

Class : ParamController

package com.c61.controller;

import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.c61.entity.User;
import com.c61.entity.ValueObject;

/**
 * 
 * @author admin
 * 收参测试  controller
 */
@Controller
@RequestMapping(value="/mvc")//等价于namespace
public class ParamController {
    /**
     * 
     * @param name
     * @param id
     * @param birth
     * @return
     * 零散传参:请求参数和方法参数同名即可
     */
    //url?id=61&name=zhoushanlin&birth=2016/09/29 11:15:40
    @RequestMapping("/param")//等价于<action name="mvc1"
    public String testMVC(String name,Integer id,Date birth){
        System.out.println(name+" "+id+" "+birth);
        return "index";
    }
    
    /**
     * 
     * @param name
     * @param id
     * @param birth
     * @return
     * 零散传参
     * 细节:
     *  @RequestParam(required=true,defaultValue="lime",value="abc")
     *      required=参数是否是必需传递的,如果参数上加了此注解,则required默认为true
     *      defaultValue=参数的默认值,如果参数没有收到数据,则取默认值
     *      value=定制请求参数的名称
     *  @DateTimeFormat(pattern="yyyy-MM-dd")
     *      pattern=定制日期的收参格式
     */
    //url?id=61&name=zhoushanlin&birth=2016/09/29 11:15:40
    @RequestMapping("/param2")//等价于<action name="mvc1"
    public String testMVC2(@RequestParam(required=true,defaultValue="lime") String name,
                           @RequestParam Integer id,
                           @RequestParam(value="b") @DateTimeFormat(pattern="yyyy-MM-dd")Date birth){
        System.out.println(name+" "+id+" "+birth);
        return "index";
    }
    /**
     * 以实体为整体
     * url?id=61&name=c61&birth=2016/09/29
     * *请求参数名 要和 实体属性名同名即可
     */
    @RequestMapping("/param3")//等价于<action name="mvc1"
    public String testMVC3(User user){
        System.out.println(user);
        return "index";
    }
    /**
     * url?hobby61=xxx&hobby61=xxx&hobby61=xxxx
     * 以数组为整体1
     */
    @RequestMapping("/param4")//等价于<action name="mvc1"
    public String testMVC4(String[] hobby61){
        for(String hobby:hobby61){
            System.out.println(hobby);
        }
        return "index";
    }
    /**
     * 以集合为整体  (了解)
     * url?users[0].id=1&users[0].name=c61&users[0].birth=2016-09-29
     *     &users[1].id=2&users[1].name=c61&users[1].birth=2016-09-29
     *     &users[2].id=3&users[2].name=c61&users[2].birth=2016-09-29
     */
    @RequestMapping("/param5")//等价于<action name="mvc1"
    public String testMVC5(ValueObject vo61){
        for(User user:vo61.getUsers()){
            System.out.println(user);
        }
        return "index";
    }
}

3 配置视图

View : index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    This is my JSP page. <br>
  </body>
</html>

零散收参 : 请求参数名 要和 方法参数名同名即可

View : param.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    <form method="post" action="${pageContext.request.contextPath}/mvc/param">
        <input type="text" name="id"/>
        <input type="text" name="name"/>
        <input type="text" name="birth"/><br/>
        <input type="submit" value="提交"/>
    </form>
  </body>
</html>

Console : 

实体为整体收参 : 请求参数名 要和 实体属性名同名即可

View : param.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    <form method="post" action="${pageContext.request.contextPath}/mvc/param3">
        <input type="text" name="id"/>
        <input type="text" name="name"/>
        <input type="text" name="birth"/><br/>
        <input type="submit" value="提交"/>
    </form>
  </body>
</html>

Console : 

数组为整体收参 : 

View : param.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    <form method="post" action="${pageContext.request.contextPath}/mvc/param4">
        <label><input name="hobby61" type="checkbox" value="hiking" />徒步旅行</label> 
        <label><input name="hobby61" type="checkbox" value="swimming" />游泳</label> 
        <input type="submit" value="提交"/>
    </form>
  </body>
</html>

Console : 

集合为整体收参 : 

View : param.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    <form method="post" action="${pageContext.request.contextPath}/mvc/param5">
    用户A:<br>
        <input name="users[0].id" type="text" value="" />
        <input name="users[0].name" type="text" value="" />
        <input name="users[0].birth" type="text" value="" /><br>
    用户B:<br>
        <input name="users[1].id" type="text" value="" />
        <input name="users[1].name" type="text" value="" />
        <input name="users[1].birth" type="text" value="" /><br>
        <input type="submit" value="提交"/>
    </form>
  </body>
</html>

Console : 

收参乱码 : 

1>get请求:在服务器的配置中添加URIEncoding="utf-8"
2>post请求:在项目中注册CharacterEncodingFilter
<!-- 专治Post请求参数乱码 -->
  <filter>
      <filter-name>encoding61</filter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      <!-- 将请求的编码方式设置为utf-8 -->
      <init-param>
          <param-name>encoding</param-name>
          <param-value>utf-8</param-value>
      </init-param>
  </filter>
  <filter-mapping>
      <filter-name>encoding61</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>

 

 

啦啦啦

啦啦啦

啦啦啦

基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究(Matlab代码实现)内容概要:本文围绕“基于数据驱动的Koopman算子的递归神经网络模型线性化”展开,旨在研究纳米定位系统的预测控制问题,并提供完整的Matlab代码实现。文章结合数据驱动方法与Koopman算子理论,利用递归神经网络(RNN)对非线性系统进行建模与线性化处理,从而提升纳米级定位系统的精度与动态响应性能。该方法通过提取系统隐含动态特征,构建近似线性模型,便于后续模型预测控制(MPC)的设计与优化,适用于高精度自动化控制场景。文中还展示了相关实验验证与仿真结果,证明了该方法的有效性和先进性。; 适合人群:具备一定控制理论基础和Matlab编程能力,从事精密控制、智能制造、自动化或相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于纳米级精密定位系统(如原子力显微镜、半导体制造设备)中的高性能控制设计;②为非线性系统建模与线性化提供一种结合深度学习与现代控制理论的新思路;③帮助读者掌握Koopman算子、RNN建模与模型预测控制的综合应用。; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法实现流程,重点关注数据预处理、RNN结构设计、Koopman观测矩阵构建及MPC控制器集成等关键环节,并可通过更换实际系统数据进行迁移验证,深化对方法泛化能力的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值