Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在SpringMVC中用html文件发送dto_Java_Spring_Spring Boot_Spring Mvc - Fatal编程技术网

Java 在SpringMVC中用html文件发送dto

Java 在SpringMVC中用html文件发送dto,java,spring,spring-boot,spring-mvc,Java,Spring,Spring Boot,Spring Mvc,我当前的控制器正在返回HTML页面。我想通过DTO发送一些数据,这些数据可以在前端接收,就像在ajax+RestController中发生的ajax响应一样。但这不是RestController,它是返回HTML的普通@controller。我不知道是否可以用html发送DTO。没有太多代码可供查看。我只是在寻找可行性。如果需要其他信息,请务必告诉我。谢谢 编辑 这里有一些代码供参考。login.html是我要返回的页面。我想以DTO的形式随此报税表发送一些附加信息 @Controller pu

我当前的控制器正在返回HTML页面。我想通过DTO发送一些数据,这些数据可以在前端接收,就像在ajax+RestController中发生的ajax响应一样。但这不是RestController,它是返回HTML的普通@controller。我不知道是否可以用html发送DTO。没有太多代码可供查看。我只是在寻找可行性。如果需要其他信息,请务必告诉我。谢谢

编辑

这里有一些代码供参考。login.html是我要返回的页面。我想以DTO的形式随此报税表发送一些附加信息

@Controller
public class LoginUserController {

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

}

如果您想在您的案例中只返回视图,即jsp。您可以将DTO对象放在modelMap中并在jsp中使用。或者简单地说,如果您只想返回JSON,您可以使用它

在登录JSP中,您可以使用dToObject

或者在Html文件中

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

似乎您正在尝试构建一个spring boot web应用程序,以下是您需要做的最低限度的工作:

确保您的maven依赖项在pom.xml中包含spring boot starter thymeleaf和spring boot starter webapp

<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>
确保您有src/main/resources/templates/login.html,它可能如下所示:

   <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>
您可以使用您选择的任何其他观看技术来代替Thymeleaf。基本上,查看技术解析HTML模板,查找如下表达式

th:文本

重温爱的价值

${message}

使用您在控制器中的模型中输入的数据


希望这有帮助

以html字符串形式返回响应,并从ajax调用JqueryObject.htmlrespose。txt@NallaSrinivas请参阅编辑和建议更改。
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>
@Controller
public class LoginUserController {

 @GetMapping(value = {"/loginuser", "/loginuser?error"})
 public String login( Model model) {
   model.addAttribute("message", "Aww snaps! something went wrong...");
   return "login";
 }

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