英文:
spring boot - displaying object data from controller in JSP
问题
我有一个User类,两个JSP页面和一个控制器。我想要注册一个用户,将他们的详细信息存储在数据库(H2)中,并在注册后显示用户的名字。
这是我的```home.jsp```页面 -
```jsp
<form action="addUser">
<label>Name: </label><input type="text" name="name"/>
<label>Id: </label><input type="number" name="id"/>
<input type="submit" />
</form>
这是我的控制器 -
package com.example.demo;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HomeController {
@Autowired
UserRepo usrrepoobj;
@RequestMapping("/")
public String home() {
return "home.jsp";
}
@RequestMapping("addUser")
public ModelAndView addUser(@RequestParam int id, User usr) {
usrrepoobj.save(usr);
User obj = usrrepoobj.findById(id).orElse(null);
ModelAndView mv = new ModelAndView();
mv.addObject(obj);
mv.setViewName("afterRegister.jsp");
//甚至这个也不起作用
// usrrepoobj.save(usr);
// User obj = usr;
// ModelAndView mv = new ModelAndView();
// mv.addObject(obj);
// mv.setViewName("afterRegister.jsp");
//甚至这个也不起作用
return mv;
}
}
这是我的afterRegister.jsp
页面 -
Registered as ${obj.name}
对象已添加到数据库。在显示数据时我做错了什么?
<details>
<summary>英文:</summary>
I have a User class, two JSP and one controller. I want to register a user, store their details in a database (H2) and display user's name after registration.
This is my ```home.jsp``` -
<form action="addUser">
<label>Name: </label><input type="text" name="name"/>
<label>Id: </label><input type="number" name="id"/>
<input type="submit" />
</form>
This is my controller -
package com.example.demo;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HomeController {
@Autowired
UserRepo usrrepoobj;
@RequestMapping("/")
public String home()
{
return "home.jsp";
}
@RequestMapping("addUser")
public ModelAndView addUser(@RequestParam int id, User usr)
{
usrrepoobj.save(usr);
User obj = usrrepoobj.findById(id).orElse(null);
ModelAndView mv = new ModelAndView();
mv.addObject(obj);
mv.setViewName("afterRegister.jsp");
//EVEN THIS DOESNT WORK
// usrrepoobj.save(usr);
// User obj = usr;
// ModelAndView mv = new ModelAndView();
// mv.addObject(obj);
// mv.setViewName("afterRegister.jsp");
//EVEN THIS DOESNT WORK
return mv;
}
}
This is my ```afterRegister.jsp``` -
Registered as ${obj.name}
The object gets added to the database. What am I doing wrong when it comes to displaying the data in the next JSP?
</details>
# 答案1
**得分**: 1
很可能你不知道已添加用户的ID,这就是为什么你的`findById`始终返回`null`的原因。只需从`save()`方法中返回用户即可。展示一下你的用户实体。
```java
@RequestMapping("addUser")
public String addUser(@RequestParam int id, User usr, Model model)
{
User obj = usrrepoobj.save(usr);
model.addAttribute("obj", obj);
return "afterRegister.jsp";
}
请注意,代码部分没有被翻译。
英文:
Propably you don't know the ID of added user, thats why your findById
return always null
. Just return user from save()
method. Show your entity of user.
@RequestMapping("addUser")
public String addUser(@RequestParam int id, User usr, Model model)
{
User obj = usrrepoobj.save(usr);
model.addAttribute("obj", obj);
return "afterRegister.jsp";
}
答案2
得分: 0
在控制器中进行以下更改:
usrrepoobj.save(usr);
User obj = usrrepoobj.findById(id).orElse(null);
ModelAndView mv = new ModelAndView();
mv.addObject("object", obj); // 将此处更改
mv.setViewName("afterRegister.jsp");
在 afterRegister.jsp
文件中:
已注册为 ${object.name}
英文:
Make the below change in controller:
usrrepoobj.save(usr);
User obj = usrrepoobj.findById(id).orElse(null);
ModelAndView mv = new ModelAndView();
mv.addObject("object",obj); //change this
mv.setViewName("afterRegister.jsp");
In afterRegister.jsp -
Registered as ${object.name}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论