# Default implementation classes for DispatcherServlet's strategy interfaces.
# Used as fallback when no matching beans are found in the DispatcherServlet context.
# Not meant to be customized by application developers.
org.springframework.web.servlet.LocaleResolver=org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver
org.springframework.web.servlet.ThemeResolver=org.springframework.web.servlet.theme.FixedThemeResolver
org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,\
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping,\
org.springframework.web.servlet.function.support.RouterFunctionMapping
org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter,\
org.springframework.web.servlet.function.support.HandlerFunctionAdapter
org.springframework.web.servlet.HandlerExceptionResolver=org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver,\
org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver,\
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver
org.springframework.web.servlet.RequestToViewNameTranslator=org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator
org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver
org.springframework.web.servlet.FlashMapManager=org.springframework.web.servlet.support.SessionFlashMapManager
将返回的字符串与视图解析器的前后缀拼接后进行页面跳转
分为:forward(默认)请求转发和 redirect 重定向
package com.example.mvc1.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class BaseController {
@RequestMapping("/test")
public String test(){
System.out.println("first test....");
return "redirect:test";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- controller的组件扫描-->
<context:component-scan base-package="com.example.mvc1.controller"></context:component-scan>
<!-- <context:component-scan base-package="com">-->
<!--<!– 包括的过滤器–>-->
<!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>-->
<!--<!– 不包括的过滤器–>-->
<!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>-->
<!-- </context:component-scan>-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 设置前缀-->
<property name="prefix" value="/WEB-INF/html/"></property>
<!-- 设置后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
在ModelAndView中可以设置模型数据,设置视图名称等
语法
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject(key,value);
--------------------------------------------------
modelAndView.addObject("user","zhangsan");
然后我们只要在jsp页面中用$表达式进行接收key即可展现对应的value
语法
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName(页面名称);
-----------------------------------------------
modelAndView.setViewName("page");
当然设置的页面名称是可以结合之前xml中设置的前缀和后缀进行拼合的!
以new对象的方式
package com.example.mvc1.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class BaseController {
@RequestMapping("/test2")
public ModelAndView test2(){
//Model用于封装数据,View用于展示数据
ModelAndView modelAndView = new ModelAndView();
//设置视图名称
modelAndView.setViewName("test2");
return modelAndView;
}
}
以容器注入的方式让Spring自动创建
@RequestMapping("/test3")
public ModelAndView test3(ModelAndView modelAndView){
modelAndView.setViewName("test3");
return modelAndView;
}
response.getWriter().print( "data" )
回写数据,此时不需要视图跳转,业务方法返回值为voidpackage com.example.mvc1.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@RestController
public class DataController {
@GetMapping("/data")
public void test1(HttpServletResponse response) throws IOException {
response.getWriter().println("data feedback");
}
}
package com.example.mvc1.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@RestController
public class DataController {
@GetMapping("/data2")
@ResponseBody
public String test2(){
return "data2 feedback" ;
}
}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.13.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.2.2</version>
</dependency>
package com.example.mvc1.controller;
import com.example.mvc1.pojo.DataPojo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
@RestController
public class DataController {
@GetMapping("/data3")
@ResponseBody
public String test3() throws JsonProcessingException {
DataPojo dataPojo = new DataPojo();
dataPojo.setId(1);
dataPojo.setName("zhangsan");
ObjectMapper objectMapper = new ObjectMapper();
String feedback = objectMapper.writeValueAsString(dataPojo);
return feedback;
}
}
我们需要配置RequestMappingHandlerAdapter
指定setMessageConverters方法中传入的参数list为MappingJackson2HttpMessageConverter
即可让sprringMVC帮助我们直接将对象转为json
下面是MessageConverters的set方法
public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
this.messageConverters = messageConverters;
}
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters" >
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
</list>
</property>
</bean>
@GetMapping("/data4")
@ResponseBody
public DataPojo test4() {
DataPojo dataPojo = new DataPojo();
dataPojo.setId(2);
dataPojo.setName("lisi");
return dataPojo;
}
简化上方的配置处理器映射器让SpringMVC直接转成json格式中对xml进行的配置
我们可以使用mvc注解驱动的方式进行代替
<mvc:annotation-driven></mvc:annotation-driven>
在SpringMVC的各个组件中,处理器映射器、处理器适配器、视图解析器称为SpringMVC的三大组件。
使用mvc:annotation-driven自动加载RequestMappingHandlerMapping (处理映射器)和RequestMappingHandlerAdapter (处理适配器),可用在Spring-xml.xm配置文件中使用mvc:annotation-driven昔代注解处理器和适配器的配置。
同时使用默认底层就会集成ackson进行对象或集合的json格式字符串的转换
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- controller的组件扫描-->
<context:component-scan base-package="com.example.mvc1.controller"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<!-- <context:component-scan base-package="com">-->
</beans>
package com.example.mvc1.controller;
import com.example.mvc1.pojo.DataPojo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
@RestController
public class DataController {
@GetMapping("/data4")
@ResponseBody
public DataPojo test4() {
DataPojo dataPojo = new DataPojo();
dataPojo.setId(2);
dataPojo.setName("lisi");
return dataPojo;
}
}