java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'subscription' available as request attribute

StackOverflow https://stackoverflow.com/questions/22838137

  •  26-06-2023
  •  | 
  •  

Pregunta

I started working with thymeleaf.

I am following this page: Spring MVC view layer: Thymeleaf vs. JSP

I have got a class:

public class MyMainObject {
    private String a;
    private String b;
    private String c;
    private String d;
    private String e;

// getters and setters
}

I also have got a controller:

@Controller
public class MyMainObjectController extends AbstractController 
    @RequestMapping({"/subscribeth"})
    public String getObj(final MyMainObject subscription) {
        return "subscribeth";
    }
}

Here is my html code:

<form action="#" th:object="${subscription}" th:action="@{/subscribeth}">
    <fieldset>
        <div>
            <label for="a" th:text="#{subscription.a}">a:
            </label> <input type="text" th:field="*{a}" />
        </div>
        <div>
            <label for="b" th:text="#{subscription.b}">b:
            </label> <input type="text" th:field="*{b}" />
        </div>
        <div>
            <label for="c" th:text="#{subscription.c}">c: </label>
            <input type="text" th:field="*{c}" />
        </div>
        <div>
            <label for="d" th:text="#{subscription.d}">d:
            </label> <input type="text" th:field="*{d}" />
        </div>
        <div>
            <label for="e" th:text="#{subscription.e}">e:
            </label> <input type="text" th:field="*{e}" />
        </div>
        <div class="submit">
            <button type="submit" name="save" th:text="#{subscription.submit}">Subscribe me!</button>
        </div>
    </fieldset>
</form>

When I run mu application I have an error:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring3.processor.attr.SpringInputGeneralFieldAttrProcessor'
org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring3.processor.attr.SpringInputGeneralFieldAttrProcessor'
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'subscription' available as request attribute

It looks like, I have to create bean subscription.

And here is my question, how I can do that? I downloaded source code from the tutorial I mentioned before and I cannot find it.

Thank you in advance

¿Fue útil?

Solución

Annotate your controller handler method's parameter with @ModelAttribute("subscription"). By default, without the value given, Spring will generate an attribute name based on the type of the parameter. Thus MyMainObject will become myMainObject.

Otros consejos

I am a beginner, when I had this problem, It was solved by adding model.addAttribute("modelname", new modelObj()); before returning the view-name, eg:

@RequestMapping(value="/login", method=RequestMethod.GET)
public String showLogin(Model model, HttpSession session)
{
    model.addAttribute("userForm", new UserForm());
    return "users";
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top