1

I am facing problem trying print at the same time a form and array in the same HTML.

Here is my usuario class:


public class usuario {

    private String user_id;
    private String createdAt;
    private String address;
    private String latitude;
    private String longitude;
    private String birthday;
    private String email;
    private String user_idp;
    private ArrayList pedido;
    private String phoneNunmber;
    private Integer edad;
    public String nombre;
    private String confirmationCode;
    private String promotions;

    public String ciudad;

    public String getCiudad() {
        return ciudad;
    }

    public void setCiudad(String ciudad) {
        this.ciudad = ciudad;
    }

    public String getPromotions() {
        return promotions;
    }

    public void setPromotions(String promotions) {
        this.promotions = promotions;
    }

    public String getConfirmationCode() {
        return confirmationCode;
    }

    public void setConfirmationCode(String confirmationCode) {
        this.confirmationCode = confirmationCode;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public Integer getEdad() {
        return edad;
    }

    public void setEdad(Integer edad) {
        this.edad = edad;
    }

    public String getPhoneNunmber() {
        return phoneNunmber;
    }

    public void setPhoneNunmber(String phoneNunmber) {
        this.phoneNunmber = phoneNunmber;
    }

    public String getUser_idp() {
        return user_idp;
    }

    public void setUser_idp(String user_idp) {
        this.user_idp = user_idp;
    }

    public ArrayList getPedido() {
        return pedido;
    }

    public void setPedido(ArrayList pedido) {
        this.pedido = pedido;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getUser_id() {
        return user_id;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    public String getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(String createdAt) {
        this.createdAt = createdAt;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }



}

Here is my Controller code:

@Controller
public class GreetingController {


    @GetMapping("/allusers")
    public String greetingForm(Model model) throws ParseException{
        tablausuario tu = new tablausuario();
        ArrayList<usuario> user = tu.listausuarios();
        Collections.sort(user, (o1, o2) -> o2.getCreatedAt().compareTo(o1.getCreatedAt())); //tabla ordenada
        model.addAttribute("TodosLosUsuarios", user);        
        return "greeting";
    }

    @PostMapping("/allusers")
    public String greetingSubmit(Model model, HttpServletRequest request, @ModelAttribute usuario users) {
        String ciudad = request.getParameter("ciudad");
        tablausuariofilterbycity tufc = new tablausuariofilterbycity();
        ArrayList<usuario> userbycity = tufc.listausuariosfiltradosporciudad(ciudad);
        model.addAttribute("TodosLosUsuarios", userbycity);
        return "result";
    }

}


And here is my HTML "greeting"



        <div class="container">
            <h2>Listado de usuarios </h2> 
            <p th:text=" ${TodosLosUsuarios.size()} "></p>    
            <div id="capa"> </div> 
            <h1>Form</h1>
            <form action="#" th:action="@{/allusers}" th:object="${TodosLosUsuarios}" method="post">
                <p>Message: <input type="text" th:field="*{ciudad}" /></p>
                <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
            </form>

            <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Buscar por ciudad..">
            <table id="myTable" class="table table-bordered">
                <thead>
                    <tr>
                        <th>Nombre</th>
                        <th>Email</th>
                        <th>Teléfono</th>
                        <th>Día creado</th>
                        <th>Edad</th>
                        <th>Confirmado</th>
                        <th>Promocion</th>
                        <th>Direccion</th>

                    </tr>
                </thead>
                <tbody  th:each="usuariosTotales: ${TodosLosUsuarios}" >
                    <tr>
                        <td th:text=" ${usuariosTotales.getNombre()} " ></td>
                        <td th:text=" ${usuariosTotales.getEmail()} " ></td>
                        <td th:text=" ${usuariosTotales.getPhoneNunmber()} " ></td>
                        <td th:text=" ${usuariosTotales.getCreatedAt()} " ></td>
                        <td th:text=" ${usuariosTotales.getEdad()} " ></td>
                        <td th:text=" ${usuariosTotales.getConfirmationCode()} " ></td>
                        <td th:text=" ${usuariosTotales.getPromotions()} " ></td>
                        <td th:text=" ${usuariosTotales.getAddress()} " ></td>

                    </tr>

                </tbody>
            </table>
        </div>


    </div>

I am sure that the problem is in the form:


    <form action="#" th:action="@{/allusers}" th:object="${TodosLosUsuarios}" method="post">
        <p>Message: <input type="text" th:field="*{ciudad}" /></p>
        <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>

If instead of "user" I use "new usuario"


        model.addAttribute("TodosLosUsuarios", user);        

I use


        model.addAttribute("TodosLosUsuarios", new usuario());        

the form works but I am not able to read the ArrayList, and I get this error:


Caused by: org.attoparser.ParseException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "greeting" - line 10, col 40)
    at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393)
    at org.attoparser.MarkupParser.parse(MarkupParser.java:257)
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230)
    ... 48 more
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "greeting" - line 10, col 40)
    at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:117)
    at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95)
    at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633)
    at org.thymeleaf.engine.ProcessorTemplateHandler.handleStandaloneElement(ProcessorTemplateHandler.java:918)
    at org.thymeleaf.engine.TemplateHandlerAdapterMarkupHandler.handleStandaloneElementEnd(TemplateHandlerAdapterMarkupHandler.java:260)
    at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler$InlineMarkupAdapterPreProcessorHandler.handleStandaloneElementEnd(InlinedOutputExpressionMarkupHandler.java:256)
    at org.thymeleaf.standard.inline.OutputExpressionInlinePreProcessorHandler.handleStandaloneElementEnd(OutputExpressionInlinePreProcessorHandler.java:169)
    at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler.handleStandaloneElementEnd(InlinedOutputExpressionMarkupHandler.java:104)
    at org.attoparser.HtmlElement.handleStandaloneElementEnd(HtmlElement.java:79)
    at org.attoparser.HtmlMarkupHandler.handleStandaloneElementEnd(HtmlMarkupHandler.java:241)
    at org.attoparser.MarkupEventProcessorHandler.handleStandaloneElementEnd(MarkupEventProcessorHandler.java:327)
    at org.attoparser.ParsingElementMarkupUtil.parseStandaloneElement(ParsingElementMarkupUtil.java:96)
    at org.attoparser.MarkupParser.parseBuffer(MarkupParser.java:706)
    at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:301)
    ... 50 more
Caused by: org.springframework.beans.NotReadablePropertyException: Invalid property 'ciudad' of bean class [java.util.ArrayList]: Bean property 'ciudad' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
    at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:622)
    at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:612)
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:158)
    at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903)
    at org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227)
    at org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:306)
    at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:253)
    at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:227)
    at org.thymeleaf.spring5.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.java:174)
    at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74)
    ... 63 more

3
  • There are multiple thigns wrong. First you should be using @ModelAttribute instead of @RequestBody (not sure why you want to body instead of mapping it to an object). Your form doesn't use the thymeleaf tags to bind to an object/path but is a plain HTML form. Commented Jan 6, 2020 at 11:29
  • I did some changes and it kind works but now I am not able to read the array probably because I am not binding correctly thymeleaf tags in th:each Commented Jan 6, 2020 at 17:31
  • Your table isn't part of your form so isn't submitted, nor will it ever as only input types will be submitted. Commented Jan 6, 2020 at 18:23

2 Answers 2

1

I think the problem is your are using plain HTML (ie tag form) instead of the Spring JSP Form

<form:form method="POST" action="/todoslosusuarios"
modelAttribute="TodosLosUsuarios">
Sign up to request clarification or add additional context in comments.

Comments

0

I think you are missing the proper tags for action in Thymeleaf and also the "post" method declaration.

<form action="#" th:action="@{/todoslosusuarios}" method="post">

this link covers the process end to end. https://spring.io/guides/gs/handling-form-submission/

2 Comments

Following your comment, it worked, although not as desired. The problem I have is the "GET" method. In this method if I create a user the form appears --> model.addAttribute("TodosLosUsuarios", new usuario()); but if I use the arrayList "user" it not works. model.addAttribute("TodosLosUsuarios", user);
todoslosusuarios its a collection right? and you are passing a single user...you can not iterate a inslge user in a for each in the html. the example in the link works with a single object, but in your case first you need to create a colection, and then add users to it, so in your html you work with those users.

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.