当前位置: 首页 > 知识库问答 >
问题:

bean名称'watchlist'的BindingResult和普通目标对象都不能作为请求属性使用

孟翰海
2023-03-14

我一直试图提交一个html表单到spring boot,但无法使其工作。

我的pom.xml中有这个

<!-- Enable JSP with Spring Boot  -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>       

我的application.properties文件中有以下内容:

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>  

我还有一个名为index.html的类似html文件,位于

<project-name>/src/main/resources/static/index.html

代码似乎无法识别taglib,因为我在web页面中看到了以下内容:

注意taglib是如何不被识别的,只是打印在页面的顶部?

Whitelabel错误页面此应用程序没有/Error的显式映射,因此您将其视为一种后退。

Sun Oct 18 11:11:20 MDT 2020出现意外错误(Type=内部服务器错误,Status=500)。bean名称'watchlist'的BindingResult和普通目标对象都不能作为请求属性使用

以下是jsp文件中发送表单的部分:

<dialog id="favDialog">
   <form:form method="POST" action="/api/v1/AddSymbolToWatchlist" modelAttribute="watchlist">
      <form:label path="symbol">Enter Symbol:</form:label>
      <form:input path="symbol"/><br><br>
      <form:button>Add</form:button>
   </form:form>
</dialog>
@RequestMapping(value = "/AddSymbolToWatchlist", method = RequestMethod.POST)
public String addSymbolToWatchlist(@Valid @ModelAttribute("watchlist") Watchlist watchlist,
        BindingResult result, ModelMap modelMap) {
    if (result.hasErrors()) {
        return "error";
    }
    System.out.println("symbol="+watchlist.getSymbol());

    return watchlistService.addSymbolToWatchlist(watchlist);
}
    @RequestMapping(value = "/AddSymbolToWatchlist", method = RequestMethod.POST)
    public String addSymbolToWatchlist(@ModelAttribute("watchlist") Watchlist watchlist, Model m){
        String symbol = watchlist.getSymbol();
        System.out.println("symbol=" +symbol);
        Watchlist wlist = new Watchlist();
        wlist.setSymbol(symbol);
        m.addAttribute("msg", symbol +" was submitted.");
        m.addAttribute("watchlist", wlist);

        return "index";
    }

class Watchlist {
     String symbol;
     
     public String getSymbol() {
         return symbol;
     }
     public void setSymbol(String sym) {
         this.symbol = sym;
     }
}

而index.jsp中的弹出表单如下所示:

<dialog id="favDialog">
   <h2>Message: ${msg}</h2>
   <form:form method="POST" action="/api/v1/AddSymbolToWatchlist" modelAttribute="watchlist">
      <form:label path="symbol">Enter Symbol:</form:label>
      <form:input path="symbol"/><br><br>
      <form:button>Add</form:button>
   </form:form>
</dialog>

共有1个答案

姜志行
2023-03-14

如果在jsp上使用modelattribute=“watchlist”,则必须返回具有属性watchlist的模型,如下所示

 @GetMapping("/")
 String index(Model m) {
   // you should have this so that modelAttribute="watchlist" on jsp renders
   m.addAttribute("watchlist", new Watchlist()); 

   return "myjspform";
 }

下面是工作代码:

package soquestion64416269;

import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@Controller
@RequiredArgsConstructor
class Ctrl {

    @GetMapping("/")
    String index(Model m) {
        m.addAttribute("msg", "Enter symbol and click Add");
        m.addAttribute("watchlist", new Watchlist());

        return "myjspform";
    }

    @PostMapping(value = "/AddSymbolToWatchlist")
    public String addSymbolToWatchlist(@ModelAttribute("watchlist") Watchlist watchlist, Model m) {
        m.addAttribute("msg", watchlist.getSymbol() + " was submitted. Now enter new symbol and click Add");
        m.addAttribute("watchlist", new Watchlist());

        return "myjspform";
    }
}

@Data
class Watchlist {
    String symbol;
}

MyJSPForm.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html lang="en">
<body>

<h2>Message: ${msg}</h2>

<form:form method="POST" action="/AddSymbolToWatchlist" modelAttribute="watchlist">
    <form:label path="symbol">Enter Symbol:</form:label>
    <form:input path="symbol"/><br><br>
    <form:button>Add</form:button>
</form:form>

</body>
</html>
 类似资料: