Results 1 to 4 of 4
  1. #1
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default help with Thymeleaf please

    Hello people, So I was wanting some help returning a variable to a page in tymeleaf. If I could get help just converting one of my methods it would open the door for me and I'd greatly appreciate it. I tried reading the thymeleaf docs but I am having trouble with it.

    Java Code:
    @Controller
    @RequestMapping("/calc")
    public class MathController {
    	
    		private Double sum;
    		
    		private CalcFunctions calc = new CalcFunctions();
    		
    		@GetMapping(path="/add")
    		public @ResponseBody String addNums(@RequestParam double num1, @RequestParam double num2) {
    			sum = calc.add(num1, num2);
    			return "Sum = " + sum.toString();
    		}
    Java Code:
    		<h2>Add.</h2>	
    		<h3>Enter a number:</h3>
    		
    		<form action="/calc/add" method="GET">	
    			<label for="num1">number one:</label>
    			<input type="text" name="num1" th:name="*{num1}" size="50"></input>
    			<label for="num2">number two:</label>
    			<input type="text" name="num2" th:name="*{num2}" size="50"></input>
    			<input type="submit"></input>
    			
    		</form>

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: help with Thymeleaf please

    What issues are you having with the above code?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: help with Thymeleaf please

    I was having trouble returning the variable and displaying it on the view, but I figured it out. Just had to hunker down and do some reading.

    Here is my web app I have been building in a live env if you want to check it out. This is my first dynamic web app so its mainly testing stuff, but I tried making it look nice.

    It has spring security and hooked up to MySQL database

    Jason's Web Page
    Last edited by Blue2525989; 03-29-2017 at 08:35 PM.

  4. #4
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: help with Thymeleaf please

    I changed the code to this

    Java Code:
    @Controller
    public class MathController {
    
    		private CalcFunctions calc = new CalcFunctions();
    		
    		@PostMapping(path="/th-math")
    		public String addNums(
    				@ModelAttribute Numbers numbers) {
    			numbers.sum1 = 0;
    			numbers.sum1 = calc.add(numbers.getNum1(), numbers.getNum2());
    			numbers.setNum1(0);
    			numbers.setNum2(0);
    			return "th-math";
    		}
    Java Code:
    	@GetMapping("/th-math")
    	public String math(Model model) {
            model.addAttribute("numbers", new Numbers());
    		return "th-math";
    	}

    Java Code:
    	<h2>Add.</h2>	
    		<h3>Enter a number:</h3>
    		<p th:text="'sum: ' + ${numbers.sum1}" />
    		<form action="#" th:action="@{/th-math}" th:object="${numbers}" method="post">
    			<label for="num1">number one:</label>
    			<input type="text" name="num1" th:field="*{num1}" size="50"></input>
    			<label for="num2">number two:</label>
    			<input type="text" name="num2" th:field="*{num2}" size="50"></input>
    			<input type="submit"></input>
    			
    		</form>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •