(可能有不对或者解释不当的地方,望指明,谢谢)
- 关于MVC的简要概念
MVC(Model - View - Controller), 一种程序设计的理念。
SpringMVC中,一般情况下,所有的请求都会先通过前端控制器DispatcherSerlvet。其通过查询处理器映射来确定将每个请求交个那个控制器处理。控制器处理完后,携带数据模型(model)和指定视图名称交由视图解析器处理。视图解析器将给定的视图名称解析为对应的视图对象,并由该对象渲染后显示给用户。
- 搭建一个基本的SpringMVC应用(基于Java类基础的实现方式)
ps:注意看下面各个类的包名,然后将它与配置类中指定的包名联系起来
- 配置DispatcherServlet
继承AbstractAnnotationConfigDispatcherServletInitializer类,并重写三个方法(getServletMappings(),getRootConfigClasses(),getServletConfigClasses())。
package Pro.Config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class ProWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected String[] getServletMappings(){//返回应当进入当前应用的请求所对应的映射数组
return new String[]{"/"};
}
@Override
protected Class<?>[] getRootConfigClasses(){//返回用来定义和配置ContextLoaderListener应用的类
return new Class<?>[] {RootConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses(){//返回用来定义和配置DispatchServlet应用的类
return new Class<?>[] {WebConfig.class};
}
}
2.配置WebConfig
package Pro.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration //声明为配置类
@EnableWebMvc //启用SpringMVC
@ComponentScan("Pro.Controller") //启用组件扫描,并指定包名
public class WebConfig extends WebMvcConfigurerAdapter{
@Bean //声明返回的是一个bean对象
public ViewResolver viewResolver(){ //配置jsp视图解析器
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){ //配置静态资源的处理方式
configurer.enable();
}
}
3.配置RootConfig
package Pro.Config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan(basePackages = {"Pro"},
excludeFilters = {
@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)
})
public class RootConfig {
}
4.控制器的实现
package Pro.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import Pro.Pojo.TestPojo;
import static org.springframework.web.bind.annotation.RequestMethod.*;
import java.util.LinkedList;
import java.util.List;
import static java.lang.System.out;
import javax.servlet.http.HttpServletRequest;
@Controller //声明为控制器
@RequestMapping(value={"/", "/index"}) //value:声明父级的匹配路径组
public class TestController {
//@RequestMapping(value="/", method=GET)
@RequestMapping(method=GET) //未指定value,则代表其为默认的处理调用;method:声明匹配的请求类型;
public String defaultController(HttpServletRequest req){
out.println("defaultController get msg");
out.println(req.getServletContext().getRealPath(""));
out.println(req.getServletContext().getContextPath());
return "test";
}
@RequestMapping(value="/2") //value:声明子级的匹配路径组
public String controller2(){
out.println("controller2 get msg");
return "2";
}
//查询参数的传递
@RequestMapping(value="/3")
public String controller3(@RequestParam(value="name", defaultValue="jack") String tName){//RequestParam中通过value声明查询参数name,前端可以通过'请求路径?key=value'的形式来传递;defaultValue代表name的默认值,即未传递时的值,也可以不设置;
out.println("controller3 get msg");
out.println("get args:{name:"+tName+"}");
return "3";
}
//路径参数的传递
@RequestMapping(value="/pathArgsTest/{argOne}")
public String controller4(@PathVariable(value="argOne") String arg){//PathVariable中通过value声明路径参数argOne,需要配合在RequestMapping中一起使用
out.println("controller4 get msg");
out.println("get pathVairable:["+arg+"]");
return "4";
}
//表达参数的传递
@RequestMapping(value="/bodyTest", method={GET, POST})
@ResponseBody //声明后,返回的数据不会被解析为视图
public String controller6(TestPojo pojo){//通过spring自动将表达转换为对应的类实例
out.println(pojo.toString());
return "test msg";
}
@RequestMapping(value="/sendFormData", method=POST)
public String controller7(){
out.println("controller7 get message");
return "redirect:/bodyTest"; //视图解析器发现redirect前缀后会发送给定路径的重定向请求;如果是forward则是转发
}
@RequestMapping(value="/formTest", method=GET)
public String controller5(){
return "FormTest";
}
}