Thymeleaf 의 일반 양식 제출 과 AJAX 제출

 Java            :
	//String     : @NotEmpty --       max=16 --      16
	@NotEmpty(message = "songName    ")
    @Size(max = 16 , message = "songName      16")
    private String songName;

    @NotEmpty(message = "singer    ")
    @Size(max = 12 , message = "singer      12")
    private String singer;
	
	//int     : @NotNull --      min=1 max=127 --   1~127  
	@Range(min = 1, max = 127, message = "age    1~127")
    @NotNull(message = "age    ")
    private Integer age;
           ,                   。        。
        SpringMVC     ,      (insertSong.html):



    
    insert Song




!!!!


Action

    :
@PostMapping("/song/save.action")
    public ModelAndView insertSong(@Valid Song song, BindingResult result){
		//@Valid        ,
		ModelAndView modelAndView = new ModelAndView();

        System.out.println("    :"+ song.getSinger());

        if(result.hasErrors()){
            modelAndView.addObject("hintMessage", "   !");
        }else{
            String songName = song.getSongName();
            Song dataSong = songService.findSongByName(songName);
            if(dataSong != null){
                modelAndView.addObject("hintMessage", "         !");
            }else{
                modelAndView.addObject("hintMessage", "    !");
                songService.insertSong(song);
            }

        }

        modelAndView.setViewName("/success");

        return modelAndView;

    }
  ,              :
@GetMapping("/song/add")
    public ModelAndView addSong(){

        ModelAndView modelAndView = new ModelAndView();


        Song song = new Song(0, null, null);
        modelAndView.addObject("song", song);
        modelAndView.addObject("hintMessage", "     !");
        modelAndView.setViewName("/insertSong");

        return modelAndView;
    }
  insertSong.html       :
!!!!
        ,     :
	Song song = new Song(0, null, null);
    modelAndView.addObject("song", song);
    modelAndView.addObject("hintMessage", "     !");
        ,     SpringMVC    ,    url  thymeleaf      (th:action="@{/song/save.action}"),         。
    Thymeleaf ajax     :
Thymeleaf   javaS     :
	
		 /*<![CDATA[*/
			....(   js  )
		 /*]]>*/
	
      (ajaxTest.html):



    
    Thymeleaf Ajax Test

    
    

        /*<![CDATA[*/
            function ajaxSubmit(){
                var songName = $("#songName").val();
                var singer = $("#singer").val();

                $.ajax({
                    url: [[@{/song/ajaxTest.action}]],
                    type: 'post',
                    dataType: 'json',
                    contentType: 'application/json',
                    data: JSON.stringify({songName : songName,singer : singer}),
                    async: true,
                    success: function(data){
                        if(data != null){
                            alert('flag : ' + data.flag + " , hintMessage : " + data.hintMessage);
                        }
                    }
                });
            }

        /*]]>*/


    


    
    
    



        ajax url   :url: [[@{/song/ajaxTest.action}]], url   [[url]] 。
    ,    SpringMVC      :
@GetMapping("/song/ajax")
public String toAjax(){
	return "/ajaxTest";
}
    
@PostMapping("/song/ajaxTest.action")
public void ajaxTest(@RequestBody Song song, HttpServletResponse resp)throws Exception{

	String songName = song.getSongName();
    Song dataSong = songService.findSongByName(songName);

    String flagMessage = "success";
    String hintMessage="      !!!";
    if(dataSong != null){
	    flagMessage = "fail";
        hintMessage = "        !!!";
    }else{
        songService.insertSong(song);
    }

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("flag",flagMessage);
    jsonObject.put("hintMessage",hintMessage);
    System.out.println(jsonObject.toJSONString());
    resp.setContentType("text/html;charset=UTF-8");
    resp.getWriter().println(jsonObject.toJSONString());
    resp.getWriter().close();

    }
      :
![       ](https://img-blog.csdn.net/20180702193541231?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1pIQU5HTElfV09SQg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
            :
1.          String         (xxx.html),Controller     @Controller  ,   @RestController
2.springboot      /resources/static,               staic           。 :/resources/static/jquery/jquery-1.7.2.js,   Thymeleaf          : th:src="@{/jquery/jquery-1.7.2.js}"
3.      url      index.html,    index.html  static     , :/static/index.html

     Thymeleaf         ,           (SpringCloud Thymeleaf  demo),Thymeleaf              ,         。
         :      th:field      ,Thymeleaf     ,          ,               。

좋은 웹페이지 즐겨찾기