在这种情况下,如果你要访问静态页面,肯定是不能正确访问的:会出现如下错误:
那么,此时,你应该:
静态资源映射:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {//设置静态资源映射@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");}}
创建Controller:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;import javax.servlet.http.HttpServletRequest;@Controller
public class IndexController {@GetMapping({"", "/", "/index", "/index.html"})public String indexPage(HttpServletRequest request){return "index";}
}
重启项目:重新运行程序
当然,这个是仅仅能进入index.html页面,至于其他页面:比如,注册,登录:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;@Controller
public class BBSUserController {/*** 跳转到登录页面** @return 返回登录页面的视图路径*/@GetMapping({"/login", "/login.html"})public String loginPage() {return "user/login";}/*** 跳转到注册页面** @return 返回注册页面的视图路径*/@GetMapping({"/register", "/register.html"})public String registerPage() {return "user/reg";}
}
重启项目:重新运行程序
此时注册/登录页面便能够成功运行了!!
完美!
当然,本次情况仅供参考!