Sending dto with html file in Spring mvc

spring mvc button'' onclick example
how to get form data in spring mvc controller
spring mvc form submit example
spring mvc form example mkyong
spring mvc example
spring boot thymeleaf form submit example
spring mvc registration form example with database\
form:input path spring mvc

My current controller is returning to an HTML page. I want to send some data (a DTO) which can be received at front end, like an ajax response (like it happens in ajax + RestController). But this is not a RestController, it is a normal @controller returning HTML. I don't know even if it is possible to send DTO with html. There isn't much code to look at. I am just looking for feasibility. Do let me know if additional info is required. Thanks

Edit

Here is some code for reference. login.html is the page I am returning to. I want to send some additional info with this return ( in the form of a DTO ).

@Controller
public class LoginUserController {

  @RequestMapping(value = {"/loginuser", "/loginuser?error"})
  public String login() {
    return "/login";
  }

}

If you want to return view only i.e jsp in your case . You could put DTO object in modelMap and used inside jsp . Or simply if you want to return JSON only you could use @ResponseBody .

@RequestMapping(value = {"/loginuser", "/loginuser?error"})
  public String login(ModelMap model) {
   YourDTO dtoObject = // get DTO;
   model.put("dtoObject",dtoObject)
   return "/login";
 }

  class YourDTO {
    private int id;
    private String name;
    // getters setters 
  }

Inside login JSP you can usedtoObject .

     Name <c:out value="${dtoObject.name}" ></c:out>  // Using JSTL

Or In Html file

    <input type="text" id="name" value='${dtoObject.name}'/>

Spring MVC Form Handling Tutorial and Example, View: Spring MVC form tags are used to render the equivalent HTML form fields, and File: src/main/java/net/codejava/spring/model/User.java� In this tutorial we look at how to upload files using spring MVC framework. There are three things that need to be done to be enable file upload in a form. Add a file upload field in the form. This can be easily achieved by using a input element of type file. . The form needs to be configured to submit files.

Seems like you're trying to build a spring boot web app, here is the bare minimum what you need to do:

  1. Make sure your maven dependencies have spring-boot-starter-thymeleaf along with spring-boot-starter-webapp in the pom.xml

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
  2. Configure/write your controller like this :

    @Controller
    public class LoginUserController {
    
     @GetMapping(value = {"/loginuser", "/loginuser?error"})
     public String login( Model model) {
       model.addAttribute("message", "Aww snaps! something went wrong...");
       return "login";
     }
    
    }
    
  3. Ensure you have src/main/resources/templates/login.html which may look like this:

       <html xmlns:th="http://www.thymeleaf.org">
       <head>
           <title>Login Page</title>
           <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
       </head>
       <body>
            <p th:text="${message}" />
       </body>
       </html>
    

You can use any other viewing technology of your choice instead of Thymeleaf. Basically the viewing technology parses the HTML template, looks for expressions like

th:text

to reslove the value of

${message}

with the data that you've put in the model in Controller.

Hope this helps!

Getting Started, id and content form fields. A results page will be displayed when the form is submitted. by a controller. These components are easily identified by the @ Controller annotation. The following View is responsible for rendering the HTML content: package Now we can review the process of submitting the form. As noted� Now, let’s see how to build the database file upload application with Spring and Hibernate in step-by-step. 1. Creating Database Because our application is for uploading files to database so we need to create a database and a table in MySQL server. Execute the following SQL script to create a database called filedb with a table called files

you can use following code 

    @Controller
    public class LoginUserController {

     @GetMapping(value = {"/loginuser", "/loginuser?error"})
     public String login( Model model) {
       organizationDTO = organizationService.getOrganizationDTO(id);
        model.addAttribute("organizationDTO", organizationDTO);
       return "/login";
     }
}

and use below code in HTML page using JSTL

    <div class="row">
        <p class="col-md-4">${organizationDTO."your value"}</p>
    </div>

eg below 

    <div class="row">
            <label class="col-md-2">Id</label>
            <p class="col-md-4">${organizationDTO.id}</p>
            <label class="col-md-2">Name</label>
            <p class="col-md-4">${organizationDTO.name}</p>
    </div>

17. Web MVC framework - Project Metadata API Guide, Model transfer with a name/value Map supports easy integration with any view Upon initialization of a DispatcherServlet , Spring MVC looks for a file named requests for example to support processing file uploads from HTML forms. The following example shows how to use File Upload Control in forms using the Spring Web MVC framework. To start with, let us have a working Eclipse IDE in place and adhere to the following steps to develop a Dynamic Form based Web Application using the Spring Web Framework. 1 Create a project with

Spring MVC File Upload Example Tutorial, Spring File Upload,Spring MVC File Upload example MultipartFile, Notice that these files are simple HTML files, I am not using any JSP or Spring tags to field “file” and “name” are the same in the input fields, so that the data will be sent in� If we want to control the maximum file upload size, we can edit our application.properties: spring.servlet.multipart.max-file-size=128KB spring.servlet.multipart.max-request-size=128KB. We can also control whether file uploading is enabled, and the location for file upload:

Getting Started with Forms in Spring MVC, Next – let's define the actual form, and of course, the HTML file that contains it. We're going to be using a page where a new employee is� To start a Spring Boot MVC application, you first need a starter. In this sample, spring-boot-starter-thymeleaf and spring-boot-starter-web are already added as dependencies. To upload files with Servlet containers, you need to register a MultipartConfigElement class (which would be <multipart-config> in web.xml). Thanks to Spring Boot

Uploading Files with Spring MVC, In this article, we focus multipart (file upload) support in Spring MVC web applications. a simple form in which we use an HTML input tag with type='file'. We can also send additional information to the server along with the� Serving static files to the client can be done in a variety of ways, and using a Spring Controller isn't necessarily the best available option. However, sometimes the controller route is necessary – and that's what we're going to be focused on in this quick article .

Comments
  • return respose as html String and from ajax invoke JqueryObject.html(respose.txt)
  • @NallaSrinivas Please see edit and suggest changes.