简体   繁体   English

将模型属性从 jsp 传递给控制器​​ Spring

[英]Passing a model attribute from a jsp to controller Spring

I'm having trouble passing data inputted by a user from a jsp to a controller using a model attribute.我无法使用模型属性将用户输入的数据从 jsp 传递到控制器。 The error returns a null pointer exception and I do not know why it's throwing this error.该错误返回空指针异常,我不知道它为什么会抛出此错误。 The code is suppose to allow a user to input data using textboxes and then save the code inputted to a repo before a database.该代码假设允许用户使用文本框输入数据,然后将输入的代码保存到数据库之前的存储库中。 However, the code returns a null pointer exception and I do not know why it's throwing this error.但是,代码返回空指针异常,我不知道为什么会抛出此错误。

My jsp code:我的jsp代码:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Add Paper</title>

</head>
<body>
<form:form 
action="/addResearchPapers" modelAttribute="Document">

<p>Author: </p>
<input type ="text" name="author" /> <br> 
<p>Title: </p>
<input type ="text" name="title" /> <br>
<p>Short Title: </p>
<input type ="text" name="shortTitle" /> <br>
<p>CPL: </p>
<input type ="text" name="CPL" /> <br>
<p>RBMA: </p>
<input type ="text" name="RBMA" /> <br>
<p>Editor: </p>
<input type ="text" name="editor" /> <br>
<p>Other Reps: </p>
<input type ="text" name="otherReps" /> <br>
<p>Item Type: </p>
<input type ="text" name="itemType" /> <br>
<p>Location: </p>
<input type ="text" name="location" /> <br>
<p>Language: </p>
<input type ="text" name="language" /> <br>
<p>Genre: </p>
<input type ="text" name="genre" /> 
<br><br>
<input type= "submit" value="Save Paper To Database" /> 
</form:form>

</body>
</html>

My controller code:我的控制器代码:

@PostMapping("/addResearchPapers")
public String newResearchPaper(@ModelAttribute("Document") ResearchPaper Document, BindingResult result, ModelMap model) {

     model.addAttribute("Author", Document.getauthor());
     model.addAttribute("Title", Document.getTitle());
     model.addAttribute("Short Title", Document.getshortTitle());
     model.addAttribute("CPL", Document.getCPL()); 
     model.addAttribute("RBMA",Document.getRBMA()); 
     model.addAttribute("Editor", Document.geteditor());
     model.addAttribute("Other Reps", Document.getotherReps());
     model.addAttribute("Item Type", Document.getitemType());
     model.addAttribute("Location", Document.getlocation());
     model.addAttribute("Genre", Document.getgenre());


    RPrepo.save(model);
    return "homepage";
}

Since Spring is in picture, you must provide the path to all attributes to your bean else it will throw null pointer as it wouldn't know what you are sending and where.由于 Spring 在图片中,您必须为 bean 提供所有属性的路径,否则它会抛出空指针,因为它不知道您发送的内容和位置。 So in oder to overcome it, replace your所以为了克服它,更换你的

<input type ="text" name="author" />

with this:有了这个:

<form:input type ="text" path="author" name="author" />
and so on...

here "path" is your field name in your ResearchPaper Bean.这里的“路径”是您在 ResearchPaper Bean 中的字段名称。 Hope it works out for you.希望它对你有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM