springMVC基础
作者:互联网
springMVC基础
什么是springMvc
如何创建mvc项目
导入组件
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<!-- Thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<!-- Thymeleaf整合Spring -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
进行配置
代码
public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
// 这个方法是加载配置类
return new Class[] {Config.class};
}
@Override
protected String[] getServletMappings() {
//这个方法是对请求的分类
return new String[] {"*.do"};
}
@Override
protected Filter[] getServletFilters(){//这个方法是为了防止接收浏览器传过来的中文字符
return new Filter[] {new CharacterEncodingFilter("utf-8")};
}
}
@Configuration
@ComponentScan("cn.tedu.controller")
@EnableWebMvc
public class Config implements WebMvcConfigurer{
//拦截器的配置
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new DemoInterceptor())
.addPathPatterns("/home.do","/user/*")
.excludePathPatterns("/user/login.do",
"/user.reg.do",
"/user/handle_login.do");
}
//ThyMealef的配置
@Bean
public ThymeleafViewResolver viewResolver(){
System.out.println("init ThymeleafViewResolver");
//设置模板保存位置为 /resources/templates/*.html
ClassLoaderTemplateResolver templateResolver =
new ClassLoaderTemplateResolver();
//模版存储文件夹
templateResolver.setPrefix("/templates/");
//模版后缀
templateResolver.setSuffix(".html");
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCacheable(true);
//创建模板引擎
SpringTemplateEngine templateEngine =
new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
templateEngine.setEnableSpringELCompiler(true);
//创建模版解析器
ThymeleafViewResolver viewResolver =
new ThymeleafViewResolver();
viewResolver.setCharacterEncoding("UTF-8");
viewResolver.setTemplateEngine(templateEngine);
return viewResolver;
}
}
解决中文编码问题
特殊字符传递处理
利用request向view传递数据
区别转发和重定向
两者的区别
转发:客户端发起请求 服务器端的控制器和试图协作完成这次任务
重定向:是http协议的功能,客户端发起请求 ,服务器返回一个302状态码和url,客户端根据url发起第二次请求
使用
session的使用
拦截器
public class DemoInterceptor implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// TODO Auto-generated method stub
HttpSession session = request.getSession();
String username = (String)session.getAttribute("username");
if (username==null) {
String n = request.getContextPath()+"/user/login.do";
response.sendRedirect(n);
return false;
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
标签:return,springMVC,基础,request,templateResolver,Override,new,public 来源: https://blog.csdn.net/weixin_45933638/article/details/113509739