Spring Boot – 在JSP中显示来自控制器的对象数据

huangapple go评论84阅读模式
英文:

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&#39;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(&quot;/&quot;)
public String home()
{
	return &quot;home.jsp&quot;;
}

@RequestMapping(&quot;addUser&quot;)
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(&quot;afterRegister.jsp&quot;);
	
	//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(&quot;addUser&quot;)
public String addUser(@RequestParam int id, User usr, Model model)
{
    User obj = usrrepoobj.save(usr);
    model.addAttribute(&quot;obj&quot;, obj);
    
    return &quot;afterRegister.jsp&quot;;
}

答案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(&quot;object&quot;,obj); //change this
        mv.setViewName(&quot;afterRegister.jsp&quot;);

In afterRegister.jsp -

Registered as ${object.name}

huangapple
  • 本文由 发表于 2020年10月11日 17:23:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64302439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定