web开发
导入静态资源
-
webjars
- 不推荐
- 前后端分离
-
resources
- resources
- static
- public
-
优先级
- resources
- 一般放上传的文件
- static
- 静态资源、图片
- public
- 公共使用的文件
- resources
# 自定义静态资源路径
# 会覆盖原有的默认路径
spring.mvc.static-path-pattern=/xxx/**
首页
-
index.html
- 默认
-
首页定制
//templates目录下的所有页面需要controller跳转
//需要导入模板引擎thymeleaf(不推荐使用,了解即可)
@Controller
public class IndexController {
@RequestMapping({"/index","/"})
public String helloworld(){
return "hello";
}
}
- resources
- templates
- hello.html
- templates
网页图标定制
- 新版已经不使用了
# 关闭默认图标
spring.mvc.favicon.enabled=false
- resources
- statics
- favicon.ico
- statics
模板引擎
-
jsp
-
模板引擎
- thymeleaf
- freemarker
-
不推荐使用
-
前后端牵扯太深
-
推荐前端框架(UI/JS)
- vue
- react
- miniui
- easyui
- weui
扩展MVC
- 视图解析器
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Bean
public ViewResolver myViewResolver(){
return new MyViewResolver();
}
//ViewResolver
//视图解析器
//自定义视图解析器
//如果想自定义定制化功能,只需要自己实现这个ViewResolver,并放入Springboot
public static class MyViewResolver implements ViewResolver{
public View resolveViewName(String s, Locale locale) throws Exception {
return null;
}
}
}
@Configuration
//@EnableWebMvc 不能加这个注解
public class MyMvcConfig implements WebMvcConfigurer {
//视图跳转
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/error").setViewName("index");
}
}
拦截器
@Configuration
public class LoginHandlerInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//登录成功后,应该有用户的session
//登录成功后,调用HttpSession保存用户信息,比如username等
Object loginUser = request.getSession().getAttribute("username");
if(loginUser==null){
//不允许进入其他页面
//跳转登录页面
request.getRequestDispatcher("/index.html").forward(request,response);
return false;
}
return true;
}
}
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if(!registry.hasMappingForPattern("/i18n/**")){
registry.addResourceHandler("/i18n/**").addResourceLocations("classpath:/i18n/");
}
}
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/index.html","/","/login.html");
}
}
- 其他功能
- WebMvcConfigurer接口
- addInterceptors:拦截器
- addViewControllers:页面跳转
- addResourceHandlers:静态资源
- configureDefaultServletHandling:默认静态资源处理器
- configureViewResolvers:视图解析器
- configureContentNegotiation:配置内容裁决的一些参数
- addCorsMappings:跨域
- configureMessageConverters:信息转换器
- WebMvcConfigurer接口
注册servlet
- 在@Configuration配置类中注册
//注册Servlet组件
@Bean
public ServletRegistrationBean myServlet(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
servletRegistrationBean.addInitParameter("name", "javastack");
servletRegistrationBean.addInitParameter("sex", "man");
return registrationBean;
}
public class MyServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String name = getServletConfig().getInitParameter("name");
String sex = getServletConfig().getInitParameter("sex");
resp.getOutputStream().println("name is " + name);
resp.getOutputStream().println("sex is " + sex);
}
过滤器
public class LogCostFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
long start = System.currentTimeMillis();
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("Execute cost="+(System.currentTimeMillis()-start));
}
@Override
public void destroy() {
}
}
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean registFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new LogCostFilter());
registration.addUrlPatterns("/*");
registration.setName("LogCostFilter");
registration.setOrder(1);//过滤顺序,从小到大依次执行
return registration;
}
}
国际化
-
不建议用模板引擎实现国际化
-
可以在前端使用国际化
-
目录形式
- static
- js
- jquery-1.9.1.min.js
- js
- i18n
- en
- common.properties
- zh-CN
- common.properties
- jquery.i18n.propertyies.js
- language.extensions.js
- en
- static
-
参考
- https://blog.csdn.net/baidu_25343343/article/details/78683761