赞
踩
解决:beans里面导包导错了
解决:
1.Books类中创建的属性
- private int bookID;
- private String bookName;
- private int bookCounts;
- private String detail;
2.BookMapper接口的内容
发现圈红的地方与Books类中的bookID不一致所以报错
很奇葩的错误方法和前面更新一样完全看不出哪里有错误
解决:
既然是没找到方法那可能是某处的方法名称没写对
如下图这里的deleteBookId应该改成deleteBookById要和BookMapper里面的方法名一致
代码如下:
- @RestController
- public class AjaxController {
- @RequestMapping("/hello")
- public String sayHello(){
- return "hello";
- }
- }
原因:
在web.xml文件中没有加:
- <servlet>
- <servlet-name>springmvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:springmvc-servlet.xml</param-value>
- </init-param>
-
- <!--启动级别1-->
- <load-on-startup>1</load-on-startup>
- </servlet>
-
- <!--/ 匹配所有的请求:(不包括.jsp)-->
- <!--/* 匹配所有的请求:(包括.jsp)-->
- <servlet-mapping>
- <servlet-name>springmvc</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
注释没写清楚可以看:https://blog.csdn.net/mengltx/article/details/79833322
拦截器:
- public class LoginHandlerInterceptor implements HandlerInterceptor {
- @Override
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
- //登陆成功之后,应该有用户的session
- System.out.println("开始拦截");
- Object username = request.getSession().getAttribute("username");
- if (username == null){
- request.setAttribute("msg","没有权限,请先登陆");
- request.getRequestDispatcher("/admin/login.html").forward(request,response);
- return false;
- }else {
- return true;
- }
- }
- }
MvcConfig类
- @Configuration
- public class MyMvcConfig implements WebMvcConfigurer {
-
- //添加拦截器
- @Override
- public void addInterceptors(InterceptorRegistry registry) {
- //注册TestInterceptor拦截器
- System.out.println("是否注册");
- InterceptorRegistration registration = registry.addInterceptor(new LoginHandlerInterceptor());
- registration.addPathPatterns("/**"); //所有路径都被拦截
- registration.excludePathPatterns(
- "/**/*.html",
- "/**/*.js",
- "/**/*.css",
- "/**/*.woff",
- "/**/*.ttf"
- );
- }
- }
问题解析:https://blog.csdn.net/A_bad_horse/article/details/105300073
原因:
1. 首次登录会进入:http://localhost:8080/admin/toLlogin页面
2. 接下来拦截器会拦截/admin/toLogin页面判断是否有session
3. 因为没有设置/admin/下面的登陆页面不拦截所以一直重复1-2-1死循环
解决:把/admin/下的index.html不拦截如果没有session的情况下跳转到/admin/index 页面
再把视图控制器加上
- @Configuration
- public class MyMvcConfig implements WebMvcConfigurer {
- @Override
- public void addViewControllers(ViewControllerRegistry registry) {
- registry.addViewController("/").setViewName("/");
- registry.addViewController("/admin/index.html").setViewName("/admin/index");
- registry.addViewController("/admin/login.html").setViewName("/admin/login");
- }
-
- //添加拦截器
- @Override
- public void addInterceptors(InterceptorRegistry registry) {
- //注册TestInterceptor拦截器
- System.out.println("是否注册");
- InterceptorRegistration registration = registry.addInterceptor(new LoginHandlerInterceptor());
- registration.addPathPatterns("/**"); //所有路径都被拦截
- registration.excludePathPatterns(
- "/admin/index.html", // 排除system路径下所有文件
- "/images/**",
- "/**/*.html",
- "/**/*.js",
- "/**/*.css",
- "/**/*.woff",
- "/**/*.ttf"
- );
- }
- }
aritcle:
- package com.aotao.springboot.domain;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- import java.util.List;
-
- @Data
- @NoArgsConstructor
- @AllArgsConstructor
- public class Article {
- private int articleID;
- private String articleTitle;
- private String content;
- private String flag;
- private String description;
- private String imgUrl;
- private String createTime;
- private String updateTime;
- private int typeID;
-
-
- }
insert:
- <insert id="insertArticle" parameterType="Article">
- insert into article (articleTitle,content,flag,description,imgUrl,createTime,updateTime,typeID)
- values (#{articleTitle},#{content},#{flag},#{description},#{imgUrl},#{createTime},#{updateTime},#{typeID});
- </insert>
分析:
报错内容说的是insertArticle 方法返回了不支持的类型
public Article insertArticle(Article article);
返回类型为Article,我发现添加文章操作应该返回的是添加成功了几条而不是一个类型。
所以把返回类型改为int就行了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。