zoukankan      html  css  js  c++  java
  • 415 Unsupported Media Type

    测试spring mvc  @ModelAttribute 属性时报错

    jsp 文件

    <%@ page language="java" import ="java.util.*" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <script type="text/javaScript" src="${pageContext.request.contextPath }/js/jquery-3.6.0.min.js">
    </script>
    </head>
    <body>
        ${requestScope.a} <br/>
        ${sessionScope.a} <br/>
        ${requestScope.uu.name} <br/>
        ${requestScope.user.name} <br/>
        
    <form name="Form2"  id="form2" action ="" method="post"  enctype="multipart/form-data"> 
         姓名:<input type="text" name="name" id="name">
         id:<input type="text" name="id" id="id">
     <input type="button" value="测试" onclick="testJson()" />
    </form> 
    </body>
    <script type="text/javascript">
    function testJson() {
        var name = $("#name").val();
        var id = $("#id").val();
        alert("姓名"+name+",id:"+id);
        $.ajax({
            //请求路径
            url : "testModelAttribute",
            //请求类型
            type : "post",
            //data表示发送的数据
            data :{
                    "name" : name,
                    "id" : id
                }, 
            //定义回调响应的数据格式为JSON字符串,该属性可以省略
            dataType : "json",
               /* contentType :'application/json',  */
            //成功响应的结果
            success : function(data) {
                alert(data);
                if (data != null) {
                    alert("输入的用户名:" + data.name + ",密码:" + data.id);
                }
            }
        });
    }  
     
    
    </script>
    </html>

    controller 类

    @Controller
    public class TestAttribute {
    
        @Resource
        private HibernateTemplate hibernateTemplate;
        
        public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
            this.hibernateTemplate = hibernateTemplate;
        }
        @ModelAttribute
        public void getUser(@RequestParam (value ="id",required =false) String id,Map<String, Object> map) {
            System.out.println("进入了查询方法 getuser");
            User user = null ;
            if (id!=null) {
                String sql= " FROM User WHERE id = :id ";
                List<User> list= (List<User>) hibernateTemplate.findByNamedParam(sql, "id", Integer.valueOf(id));
                user = list.get(0);
            }
            map.put("user", user);
            System.out.println("user1:"+user);
        }
        /**
         *      测试 @ModelAttribute 属性 ,既如果页面上修改数据 需要保存数据 但是 不能显示敏感字段时 
         *      可以从数据查询后 返回页面这样修改数据就不会把特俗字段置空
         *      例如 测试修改 用户名字 页面又不能显示用户密码  逻辑根据页面ID去数据查询到 user 实体 返回页面,修改的时候直接用实体修改就有密码了 
         * @param user
         * @return
         */
        @RequestMapping("/testModelAttribute")
        @ResponseBody
        public  User testModelAttribute(@RequestBody User user) {
            System.out.println("user2:"+user);
            return user;
        }
    }

    看视频上说 如果 Ajax 接受参数为pojo 实体类就要加上 注解 @RequestBody 用

    原先以为时 Ajax没有配置  contentType :'application/json', 加上之后获取不到参数值 获取的user 是null

    后来去掉了 jsp 的contentType  配置 和  @RequestBody 参数 可以正常测试了,不知道时什么问题。也可能是我的jar包版本太低

    修改后的方法

        @RequestMapping("/testModelAttribute")
        @ResponseBody
        public  User testModelAttribute(User user) {
            System.out.println("user2:"+user);
            return user;
        }
  • 相关阅读:
    apache 问题 You don't have permission to access /test.php on this server 解决方法
    setTimeout和setInterval实现定时器的区别
    视图Ext.Viewport和窗口Ext.Window用法
    JavaScript设置Cookie
    布局Layout
    html中select标签刷新后不回到默认值而是保持之前选择值
    设置session失效的几种方法
    面板Ext.Panel使用
    树TreePanel
    让html元素随浏览器的大小自适应垂直居中
  • 原文地址:https://www.cnblogs.com/zjf6666/p/14627177.html
Copyright © 2011-2022 走看看