关于在SSM框架中返回的是字符串还是页面跳转的问题

如果你在控制器前的注解是@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的结合

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值