0

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

2
  • you question is not clear ...... Commented Jul 23, 2019 at 8:37
  • 1
    Updated OP, hopefully it is more clear. I want to populate the dropdown in the HTML with the 3rd method in the controller class, but the HTML doesn't parse. Moreover, the other fields in the HTML don't recieve their necessary info either from the other methods in the controller class. Commented Jul 23, 2019 at 8:41

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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'>

8 Comments

Thanks! For the controller method, I need to return all tests currently stored in the List of objects. I don't entirely understand how your method does this? Should I iterate through the List of objects, and then return it? Also, where do I add the model.addAttribute? It's not accepted in my controller method. Sorry for perhaps obvious questions, I am new to this framework :)
<form action='@{/sendTest()}' th:object="${test}" method='post'>, do I need to use th:action perhaps, and in the other tags as well?
You are right, it should be th:action. I'll correct my answer.
th:action is needed only in form tag. No need to add it to other tags.
and do I have to use @ModelAttribute annotation in the controller methods? model.addAttribute shows up as error in the code when I add it, with no resolve option available
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.