LayUI experience 1 (JS separate file cannot get $(pageContext.request.contextPath) and add Date data to database table in JS)

After working on LayUI for a day, although it is still ignorant, it is also a template change checked on GitHub, and the basic front-end and back-end additions, deletions, and changes have been completed. Let me record the problems I encountered below.

get $(pageContext.request.contextPath)

In JS, I found that I couldn't directly reference this, so my initial JS was all written in JSP, and the code was very messy.

Add a sentence to the JSP

<input type="hidden" id="context" value="${pageContext.request.contextPath}">

Then we can directly get the context in the corresponding JS file and use it as a string

var context= document.getElementById("context").getAttribute("value")

I also encountered the problem of the path. It is recommended not to put the JS file under Web-inf, because it is really difficult to access. I have checked a lot and have not seen it, and there is no expression (uncomfortable). JSP can be accessed through the view resolver in the configuration file through Action.

The json string of the obtained object is sent back to the background, and Date type data cannot be converted

Front-end code:

form.on("submit(changeUser)",function(data){
        var index = layer.msg('Submit, please wait',{icon: 16,time:false,shade:0.8});
        var userInfoHtml = '';
        userInfoHtml = {
            'id':$(".id").val(),
            'password':$(".password").val(),
            'created':$(".created").val(),
            'username' : $(".layui-disabled").val(),
            'sex' : data.field.sex,
            'status' : data.field.status,
            'phone' : $(".userPhone").val(),
            'email' : $(".userEmail").val()
        };

        var json=JSON.stringify(userInfoHtml);
        window.location=stringfy+"/user/editOneUser?json="+json;
        setTimeout(function(){
            layer.close(index);
            layer.msg("Submitted successfully!");
        },2000);
        return false; // Prevent form jumping. If you need to jump to the form, just remove this paragraph.
    })

There are a lot of mistakes I forgot to cut

Request processing failed; nested exception is com.google.gson.JsonSyntaxException

One is that Jackson doesn't seem to be able to parse it, so I changed it to Gson.Gson and it became  java.lang.NumberFormatException

Or the date in my JavaBean entity class is of Date type. I changed the entity class and replaced the parameter of Date type with String, and I can receive the Json string I passed. By adding the received entity class TBUserTest data to the original entity class TBUser.

public String editOneUser(String json) throws IOException, ParseException {
        //Jackson parses the JSon string, because of the Date type, there will always be mistakes that I can't solve later,
//        ObjectMapper objectMapper = new ObjectMapper();
//        TbUser tbUser=objectMapper.readValue(json, TbUser.class);
        Gson gson = new GsonBuilder ()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .create();
        TBUserTest tbUser = gson.fromJson(json, TBUserTest.class);
        TbUser user=new TbUser();
        SimpleDateFormat sfEnd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sfStart = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",java.util.Locale.ENGLISH) ;
        String format = sfEnd.format(sfStart.parse(tbUser.getCreated()));
        System.out.println(format);
        Date date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(format);
        user.setStatus(tbUser.getStatus());
        user.setCreated(date);
        user.setUpdated(new Date());
        user.setEmail(tbUser.getEmail());
        user.setId(tbUser.getId());
        user.setPassword(tbUser.getPassword());
        user.setUsername(tbUser.getUsername());
        user.setSex(tbUser.getSex());
        user.setPhone(tbUser.getPhone());
        userService.editUser(user);
        return "/page/user/userList";
    }
Then not very perfect solution to my last problem



 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324722056&siteId=291194637
Recommended