ModelAndView(Controller to view)

ModelAndView包含视图信息和模型数据信息。
常用方法:
(1)添加模型数据,相当于将数据添加到request中。

ModelAndView addObject(String attributeName,Object attributeValue);
ModelAndView addAllObjects(Map<String,?> modelMap); 

(2)设置视图

void setView(View view);
void setViewName(String viewName);

示例代码:

@RequestMapping("/index1")
	public ModelAndView index(String username){
		logger.info("welcome! username: " + username);
		//返回前端
		ModelAndView mView = new ModelAndView();
		mView.addObject("username", username);
		mView.setViewName("index");
		return mView;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Freemarker is a template engine that allows you to generate dynamic HTML pages. Spring Boot is a framework that simplifies the development of web applications by providing pre-configured settings and dependencies. ModelAndView is a class in Spring Boot that represents a model and view, used to pass data to a view and specify the view to be rendered. To use Freemarker with Spring Boot, you need to add the following dependencies to your project: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> ``` Next, you can create a controller class with a request mapping that returns a ModelAndView object: ``` @Controller public class MyController { @RequestMapping("/myPage") public ModelAndView myPage() { ModelAndView modelAndView = new ModelAndView("myPage"); modelAndView.addObject("message", "Hello World!"); return modelAndView; } } ``` In this example, the "myPage" template is used to render the view, and the "message" object is passed as a parameter to the template. The "message" object can be accessed in the template using the syntax ${message}. Finally, you need to create the "myPage" template in the "templates" directory of your application. Here's an example of what the template might look like: ``` <!DOCTYPE html> <html> <head> <title>My Page</title> </head> <body> <h1>${message}</h1> </body> </html> ``` When the /myPage endpoint is accessed, Spring Boot will render the "myPage" template with the "Hello World!" message.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值