如果你在控制器前的注解是@RestController
的话,返回的是controller方法返回的是值,如下所示:会返回tel的字符串形式
@RequestMapping("controller")
@RestController//这的问题
public class NoteController {
//控制器依赖于service
@Autowired
@Qualifier("noteService")
private NoteService noteService;
public void setNoteService(NoteService noteService) {
this.noteService = noteService;
}
//增加数据
@RequestMapping(value = "addNote")
public @ResponseBody String addNote(@RequestParam("id") String id,@RequestParam("content") String content,@RequestParam("groupName") String groupName,@RequestParam("createTime") String createTime,@RequestParam("title") String title,@RequestParam("subContent") String subContent,@RequestParam("tel") String tel) throws UnsupportedEncodingException{
int id1=Integer.parseInt(id);
Note note=new Note(id1, content, groupName, createTime, title, subContent, tel);
noteService.addNote(note);
System.out.println("注入成功");
return tel;
}
如果你在控制器前的注解是@Controller
的话,那么返回的是页面,如下所示,将返回到result.jsp页面
@RequestMapping("webController")
@Controller
public class WebController {
//控制器依赖于service
@Autowired
@Qualifier("noteService")
private NoteService noteService;
public void setNoteService(NoteService noteService) {
this.noteService = noteService;
}
@RequestMapping("login")
public String login(@RequestParam("userName") String userName ,@RequestParam("password") String password) {
if(userName.contentEquals("wang")&&password.equals("123")) {
System.out.println("控制里面");
return "result" ;
}
System.out.println("控制外面");
return null;
}
}
1.因为使用是@RestController注解Controller,则Controller中的方法无法返回页面,配置的视图解析器不起作用,返回的内容就是Return 里的内容
使用@Controller即可
分开写两个控制器
想返回字符串就用RestController
想返回页面就用Controller
2.原因:@RestController注解相当于@ResponseBody和@Controller的结合