Insights into Thymeleaf

Inside HTML form, only one object is allowed. Thymeleaf documentation says, 

Once inside the tag, no other th:object attribute can be specified. This is consistent with the fact that HTML forms cannot be nested. 

th:object attribute on the form needs to reference a form-backing bean.

Controller Class

	  @RequestMapping(value = {"/survey"})
	    public String takeSurvey(Principal principal, Model model){
		  model.addAttribute("userName", principal.getName());
		  model.addAttribute("listAllQuestions", qs.getAllQuestions());
		  model.addAttribute("listOptions", os.getAllOptions());
		  
	        return "survey";
	    } 
	
	  @RequestMapping(value = {"/save-survey"})
	    public String saveSurvey(Response response, Model model){
		  model.addAttribute("listOptions", os.getAllOptions());
		  
		  rs.save(response);
		  System.out.println(" saveSurvey : userID" + response.getUserID());
	        return "redirect:/user?thankyou";
	    } 



package shangrila.council.app.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "response")
public class Response {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name="responseid")
	private int ResponseID;

	@Column(name="userid", nullable=false)
	private String UserID;
	
	@Column(name="optionid", nullable=false)
    private int ChosenOptionID;
	
	@Column(name="questionid", nullable=false)
    private int QuestionID;

	public Response() {

	}
	
	public Response(int responseID, String userID, int chosenOptionID, int questionID) {
		super();
		ResponseID = responseID;
		UserID = userID;
		ChosenOptionID = chosenOptionID;
		QuestionID = questionID;
	}

	public int getResponseID() {
		return ResponseID;
	}

	public void setResponseID(int responseID) {
		ResponseID = responseID;
	}

	public String getUserID() {
		return UserID;
	}

	public void setUserID(String userID) {
		UserID = userID;
	}

	public int getChosenOptionID() {
		return ChosenOptionID;
	}

	public void setChosenOptionID(int chosenOptionID) {
		ChosenOptionID = chosenOptionID;
	}

	public int getQuestionID() {
		return QuestionID;
	}

	public void setQuestionID(int questionID) {
		QuestionID = questionID;
	}

	
}

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

         <!-- Bootstrap CSS -->
         <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" 
               integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" 
               crossorigin="anonymous">  
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>   
		<title> Add Question </title>
    </head>
    <body>
        <!-- Create a Navigation Bar -->
        <nav class="navbar navbar-expand-lg sticky-top navbar-dark bg-dark">
            <!-- Navbar content -->
            <button class="navbar-toggler" type="button" data-toggle="collapse" 
                data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup"
                aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarText">
                <ul class="navbar-nav mr-auto">
                    <li class="nav-item active">
                       <a class="nav-link" href="/">Home <span class="sr-only">(current)</span></a>
                    </li>
                    <!-- This thymeleaf atribute sec:authorize
                          will be handled in secutity handler 
                         -->
                    <li class="nav-item" sec:authorize="isAuthenticated()">
                        <a class="nav-link" href="/logout">Logout</a>
                    </li>
                </ul>
                <span class="navbar-text">
                   E-Survey for Energy Consumption
                </span>
        </nav>
        <br> <br> <br>
        <div class="container text-center">
          <div> <h1> Select the Question and Option  </h1> </div>
                <!-- Registration Successful -->
                <div th:if="${param.thankyou}">
                    <div class="alert alert-info">Thank you for taking survey!!! 
                    <a th:href="@{/logout}">Signout here</a>
                    </div>
                    
                </div>
                
          <form  action="#" th:action="@{/user/save-survey}" th:object="${response}" 
              id="responseForm" method="post" style="max-width: 600px; margin: 0 auto;">

              <input class="form-control"
                     type="text" th:value="${userName}" id="UserID" name="UserID" >
                  
             <div class="m-3">
              <div class="form-group row">
                  <label class="col-form-label col-sm-4"> Question </label>
                  <div class="col-sm-8">
                     <select 
                      
                       id="QuestionID" name="QuestionID" 
                       class="form-control" required>
                       <th:block th:each="quest : ${listAllQuestions}">
                         <option th:text="${quest.QuestionText}" th:value="${quest.questionID}"/>      
                       </th:block >
                     </select >
                  </div>
               </div  >
               
               <div class="form-group row">
                  <label class="col-form-label col-sm-4"> Question </label>
                  <div class="col-sm-8">
                     <select 
                   
                       id="ChosenOptionID" name="ChosenOptionID"
                        class="form-control" required>
                       <th:block th:each="opt : ${listOptions}">
                         <option th:text="${opt.optionText}" th:value="${opt.optionID}"/>      
                       </th:block >
                     </select >
                  </div>
               </div  >
               
               <div class="text-center p-3">
                  <input type="submit" value="Save" class="btn btn-info"/>
                  <a href="/user" class="btn btn-primary">Back</a>
               </div>
             </div>
          </form> 
        </div>
    </body>

</html>


https://stackoverflow.com/questions/25027801/how-to-set-thymeleaf-thfield-value-from-other-variable

https://stackoverflow.com/questions/25808433/hidden-field-value-blank-thymeleaf



Comments