score:0

Hi It is posible here a example

HTML Example

    <section id="registration" class="section">
        <div class="container tagline">
            <em>Register User</em><br />
            <form method="post" th:action="@{/user/register}" th:object="${newUser}">
                <div>
                    <label for="givenName">Id</label>
                    <input id="id" placeholder="Enter Id"  type="text" th:field="*{id}" />
                    <p th:if="${#fields.hasErrors('id')}" th:text="${#strings.listJoin(#fields.errors('id'), ', ')}">
                    </p> 
                </div>
                <div>
                    <label for="givenName">Name</label>
                    <input id="name" placeholder="Enter Name"  type="text" th:field="*{name}" />
                    <p th:if="${#fields.hasErrors('name')}"
                        th:text="${#strings.listJoin(#fields.errors('name'), ', ')}"></p>
                </div>
                <div>
                    <label for="email">Email</label>
                    <input id="email" placeholder="Enter the Email"  type="text" th:field="*{email}" />
                    <p th:if="${#fields.hasErrors('email')}"
                        th:text="${#strings.listJoin(#fields.errors('email'), ', ')}"></p>
                </div>
                <button type="submit">Create user</button>
            </form>

        </div>
    </section>

<div>
<form method="get" th:action="@{/user/searchById}" th:object="${searchUser}">
         <input id="id"  placeholder="Search by id"  type="text"  th:field="*{id}" size="10" >
         <button type="submit" >search</button> 

</form>

<form method="get" th:action="@{/user/searchByName}" th:object="${searchUser}">
         <input id="id"  placeholder="Search by Name"  type="text"  th:field="*{name}" size="10" >
         <button type="submit" >search</button> 

</form>

<form method="get" th:action="@{/user/searchByEmail}" th:object="${searchUser}">
         <input id="id"  placeholder="Search by Email"  type="text"  th:field="*{email}" size="10" >
         <button type="submit" >search</button> 
</form>

</div>

Spring

@Controller
@RequestMapping("/user")
public class User {

    BookingFacadeUserImp userService = new BookingFacadeUserImp();

    @GetMapping()
    public ModelAndView init() {
        return getModelView().addObject("users", getUsersByName("", 100, 0));
    }

    @PostMapping("/register")
    public ModelAndView saveUser(@Valid @ModelAttribute("newUser") UserDTO user, BindingResult result)
            throws NumberFormatException, ParseException {

        System.out.println("Register: " + user);
        userService.createUser(user);
        return getModelView().addObject("users", getUsersByName("", 100, 0));
    }

    @GetMapping("/delete/{id}")
    public ModelAndView delete(@PathVariable long id) {
        userService.deleteUser(id);
        return init();
    }

    @PostMapping("/update")
    public ModelAndView updateUser(@ModelAttribute("updateUser") UserDTO user) {
        userService.updateUser(user);
        return init();
    }

    @GetMapping("/searchById")
    public ModelAndView searchById(@ModelAttribute("searchUser") UserDTO user) {

        if (user.getId() > 0)
            return getModelView().addObject("users", getUsersById(user.getId()));
        else
            return init();
    }

    @GetMapping("/searchByName")
    public ModelAndView searchByName(@ModelAttribute("searchUser") UserDTO user) {
        if (!user.getName().equals(""))
            return getModelView().addObject("users", getUsersByName(user.getName(),100,0));
        else
            return init();
    }
    
    public ModelAndView getModelView() {
        ModelAndView model = new ModelAndView("user");
        model.addObject("newUser", new UserDTO());
        model.addObject("searchUser", new UserDTO());
        return model;
    }

    public List<UserDTO> getUsersById(long id) {

        List<UserDTO> list = new ArrayList<UserDTO>();
        list.add((UserDTO) userService.getUserById(id));
        return list;
    }

    public List<com.example.demo.task1.model.User> getUsersByName(String name, int pageSiza, int pageNum) {
        return userService.getUsersByName(name, pageSiza, pageNum);
    }

    public List<com.example.demo.task1.model.User> getUsersByEmail(String email) {
        return (List<com.example.demo.task1.model.User>) userService.getUserByEmail(email);
    }
}

Related Query

More Query from same tag