2016-09-15 54 views
1

我UserController類:使用Jparepository的方法的FindAll與ModelAndView的

package org.peronalitymeet.controller; 

import org.peronalitymeet.entity.User; 
import org.peronalitymeet.entity.request.AddUserRequest; 
import org.peronalitymeet.repository.UserRepository; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RestController; 

import java.util.List; 

@RestController 
public class UserController { 

    private UserRepository userRepository; 

    @Autowired 
    public UserController(UserRepository userRepository) { 
     this.userRepository = userRepository; 
    } 

    @RequestMapping(value="/users", method = RequestMethod.GET) 
    public List<User> findAllUsers(){ 
     return userRepository.findAll(); 
    } 

    @RequestMapping(value="/signup", method = RequestMethod.POST) 
    public void addUser(@RequestBody AddUserRequest addUserRequest){ 
     User user = new User(); 
     user.setFirstname(addUserRequest.getFirstname()); 
     user.setSurname(addUserRequest.getSurname()); 
     userRepository.save(user); 
    } 

} 

我的問題是,現在,如果我打開本地主機:8080 /用戶URL, 我會得到我的JSON格式內的所有用戶。之前我遵循Spring mvc教程,在那裏他們使用ModelAndView方法在調用URL時自動打開一個jsp頁面。

有沒有什麼方法可以使用FindAll Users()並同時打開一個jsp或者html頁面,並在那裏列出方法的結果?

回答

1

@RestController是一個元註釋,它告訴Spring將原樣返回的文本返回給用戶。這相當於將@ResponseBody @Controller添加到您的班級。

如果你想用JSP做一些事情,你的類應該看起來像下面的東西。我只是在改變端點,但這個想法無處不在。

package org.peronalitymeet.controller; 

import org.peronalitymeet.entity.User; 
import org.peronalitymeet.entity.request.AddUserRequest; 
import org.peronalitymeet.repository.UserRepository; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.Controller; 
import org.springframework.web.bind.annotation.ResponseBody; 
import org.springframework.web.servlet.ModelAndView; 


import java.util.List; 

@Controller 
public class UserController { 

    private UserRepository userRepository; 

    @Autowired 
    public UserController(UserRepository userRepository) { 
     this.userRepository = userRepository; 
    } 

    @RequestMapping(value="/users", method = RequestMethod.GET) 
    public ModelAndView findAllUsers(ModelAndView mav){ 
     List<User> users = userRepository.findAll(); 
     mav.addObject("users", users); 
     mav.setViewName("users"); 
     return mav; 
    } 

    @ResponseBody 
    @RequestMapping(value="/signup", method = RequestMethod.POST) 
    public void addUser(@RequestBody AddUserRequest addUserRequest){ 
     User user = new User(); 
     user.setFirstname(addUserRequest.getFirstname()); 
     user.setSurname(addUserRequest.getSurname()); 
     userRepository.save(user); 
    } 

} 

然後設置你叫users.jspusers可變顯示的項目視圖頁面,你應該設置。

+0

非常感謝。 –