bullfighter
bullfighter

Reputation: 427

Thymeleaf - populate dropdown menu from object list

Currently, the methods return only their own links into the required fields, ie. the last html element for available tests returns only availableTestList in the div that is supposed to list all available tests. Same for "/currentTest" and for the dropdown menu, which shows no options at all.

I started trying some fixes from here on SO, and now my html broke down completely, giving me the error:

An error happened during template parsing (template: "templates/Teacher.html")

and in java console:

"Neither BindingResult nor plain target object for bean name 'test' available as request attribute"

Any ideas?

Below is the controller code first, with the html afterwards.

@Controller
public class TeacherController {

    TestController testcont = TestController.getInstance();

    @RequestMapping(value = "sendTest", method = RequestMethod.POST)
    public String sendTest(Model model) throws IOException, ServletException{

        for(Test test : testcont.showAllTests()){
            if(test.getName().equals("selection")){
                testcont.SetActiveTest(test);
                System.out.println(testcont.getActiveTest());
                //return "Test sent successfully to students! <a href='/Teacher'>Back</a>";
            }
        }
        model.addAttribute("tests", testcont.showAllTests());
        return "sendTest";
    }

    @RequestMapping(value = "resetCurrentTest", method = RequestMethod.POST)
    public String resetCurrentTest(Model model){
        testcont.SetActiveTest(null);

        model.addAttribute("tests", testcont.showAllTests());

        return "resetCurrentTest";
    }


    @RequestMapping(value = "currentTestOptions", method = RequestMethod.GET)
    //@ModelAttribute("/currentTestOptions")
    //@GetMapping("/currentTestOptions")
    public String currentTestOptions(Model model) {

        model.addAttribute("tests", testcont.showAllTests());
        return "currentTestOptions";
    }

    @RequestMapping(value = "getActiveTest", method = RequestMethod.GET)
    public String getActiveTest(){
        return testcont.getActiveTest().toString();
    }
}

The HTML

<body>
    <p>
        <a href='/Teacher/NewTest'>New Test upload</a>
    </p>
    <div
        style='height: 150px; width: 400px; border: 1px solid #ccc; font: 16px/26px Georgia, Garamond, Serif; overflow: auto;'>
        <form th:action='${sendTest}' th:object="${tests}" method='post'>
            <fieldset>
                <label>Select test</label> 
                <select id="tests" name="tests" class="form-control" th:field="${tests}">
                    <option value="">Select test</option>
                    <option 

                    th:each="test : ${tests}"
                    th:value="${test.getName}"
                    th:text="${test.getName}"

                    ></option>
                </select>
            </fieldset>
            <input type='submit' value='Submit'>
        </form>
    </div>
    <form action='${resetCurrentTest}' method='post'>
        <input type='submit' value='Clear'>
    </form>
    <a> Current Test for students: </a>
    <p th:text="${getActiveTest}" ></p>
    <p>All available tests on server:</p>
    <div
        style='height: 200px; width: 400px; border: 1px solid #ccc; font: 16px/26px Georgia, Garamond, Serif; overflow: auto;'>
        <th:block th:each="test : ${tests}">
    </div>
</body>

in the controller, the 3rd method "currentTestOptions" is supposed to return the full list of objects, and in the HTML I am to iterate through the list using test : currentTestOptions, and then as the value retrieve the test names to show in the dropdown.

Current console error when trying to open the local page /Teacher is:

Neither BindingResult nor plain target object for bean name 'test' available as request attribute

Upvotes: 0

Views: 2254

Answers (3)

mate00
mate00

Reputation: 2955

In html file you have: <select class="form-control" th:field="${test.getName}">

Thymeleaf expects that you will pass attribute called test through model. You can do it like this:

model.addAttribute("test", yourObjectRepresentingTest);

Do this in a controller method that returns view to your html. For example:

@GetMapping("/showTests")
public String showTests(Model model) {
   // some controller logic if you need
   SampleTest sampleTest = new SampleTest(); // <- this is your backing bean object that will be bound to thymeleaf view
   model.addAttribute("test", sampleTest);

   return "showtests"; // <- this is a file name of a html containing your view
}

You may also need to add th:object to your html file:

<form th:action="@{/sendTest}" th:object="${test}" method='post'>

Upvotes: 0

Istiaque Hossain
Istiaque Hossain

Reputation: 2357

try this code

<option th:each="test : ${currentTestOptions}"
th:value="${test.getName}"
th:text="${test.getName}"></option>

for more thymeleaf-forum/Create-drop-down-list
thymeleaf-select-option

Upvotes: 1

Stephan cai
Stephan cai

Reputation: 1

Bolow is my controller code:

ModelAndView view = new ModelAndView("view/index");
UserIdentity userIdentity = (UserIdentity) request.getSession().getAttribute(SessionConstant.ACCOUNT_SESSION_KEY);
if(userIdentity == null){
    return null;
}
List<PayBill> payBills = payBillService.getBillDetailByUserId(userIdentity.getId());
if(payBills != null && payBills.size() > 0){
    view.addObject("bill",payBills.get(0));
}
return view;

Bolow is my html code:

<div class="centerBox">
        <div class="centerBox1" th:if="${bill != null}">
            <p style="color:#999;">当月水费金额</p>
            <p style="color:red;font-size:40px;" th:text="${bill.paymentAmount}">100.00</p>
        </div>
        <div class="centerBox1" th:if="${bill == null}">
            <p style="color:#999;">当月水费金额</p>
            <p style="color:red;font-size:40px;">0.00</p>
        </div>
        <button type="button" onclick="btn()" class="mui-btn mui-btn-primary" style="width: 100%;border-radius: 20px;margin:30px 0px 10px 0px" data-loading-icon="mui-spinner mui-spinner-custom" >立即缴费</button>
        <a href="#" id="sfjl"><p>往期水费记录</p></a>
        <!-- image -->
        <div class="bottomBox">
            <img src="/images/bottom.png" width="100%" alt="" />
        </div>
    </div>

Attention please, use this code th:if="${bill != null} to avoid get a null value. if it's null, it giving me the error.

Upvotes: 0

Related Questions