添加点击量Springboot+redis+aop切入controller层方法截面+return String返回.html页面出现template might not exist:不是框架配置问题

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun May 31 23:47:56 CST 2020
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template [hello], template might not exist or might not be accessible by any of the configured Template Resolvers

网上查看了很久都是一些spring关系加载扫描.html已经一些jar包等配置问题,配置一番发现不是

例如:


    spring.thymeleaf.content-type: text/html
    spring.thymeleaf.cache: false
    spring.thymeleaf.mode: LEGACYHTML5

    mvc:
      view:
        prefix: /
        suffix: .html
		<dependency>
			<groupId>net.sourceforge.nekohtml</groupId>
			<artifactId>nekohtml</artifactId>
			<version>1.9.22</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
		</dependency>
		<!--web -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<!--mybatis -->

实际问题出在返回值上:

错误代码:

   //方法执行的前后调用
  @Around("execution (* com.bootdo.home.controller.HomeController.shownews(..))")

    public void around(ProceedingJoinPoint point) throws Throwable{
    
    


        StatisticDo statisticDo = new StatisticDo();

        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        StringBuffer requestURL = request.getRequestURL();
        int idValue  = Integer.parseInt(request.getParameter("id"));
        HashMap map = new HashMap<String,Object>();
        String url = requestURL.toString()+idValue;
        System.out.println(url);
        map.put("url",url);
        map.put("newsId",idValue);
        HashOperations hashOperations = redisTemplate.opsForHash();
        if( hashOperations.get(url,idValue) == null) {
    
    
            if (statisticService.sqlSearch(url,idValue) == null) {
    
    
                statisticDo.setUrl(url);
                statisticDo.setNewsId(idValue);
                statisticDo.setOperateTime(new Date());
                statisticDo.setClickDate(new Date());
                statisticDo.setTimes(1);
                statisticService.save(statisticDo);
                statisticService.saveRedis(url, idValue, 1);
            } else {
    
    

                StatisticDo statisticDo1 = statisticService.sqlSearch(url, idValue);
                int times1 = statisticDo1.getTimes();
                statisticService.updateRedis(url, idValue,times1+1);
                statisticService.update(url,idValue);

            }
        }
        else {
    
    
            int times = (int)hashOperations.get(url, idValue);
            statisticService.updateRedis(url,idValue,times+1);
            System.out.println(times);
        }

       point.proceed();//目标方法
      

  }

改正后代码和错误原因:

如果@Around注解的方法也就是环绕的方法设置的是void等
Object proceed = point.proceed();//目标方法
执行后,不能够直接返回页面,因为@Around注解的方法并没有结束,也就不能实现跳转。出现 网页 500错误

package com.bootdo.article.statisticaop;

import com.bootdo.article.domain.StatisticDo;
import com.bootdo.article.service.StatisticService;
import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;

/**
 * @author helloLi
 * @version 1.0
 * @date 2020/5/27 10:47
 */

@Aspect
@Component
public class StatisticAop {
    
    

    @Autowired
    private StatisticService statisticService;

    @Autowired
    RedisTemplate redisTemplate;


    //方法执行前调用
   @Before("execution (* com.bootdo.home.controller..*.*(..))")
 //   @Before("execution (* com.bootdo.home.controller.HomeController.shownews())")
    public void before() {
    
    
        System.out.println("before:统计点击量的方法调用之前)");
    }

    //方法执行后调用
    @After("execution (* com.bootdo.home.controller..*.*(..))")
    public void after() {
    
    
        System.out.println("after:统计点击量的方法调用之后)");

    }

    //方法执行的前后调用
  @Around("execution (* com.bootdo.home.controller.HomeController.shownews(..))")

    public Object around(ProceedingJoinPoint point) throws Throwable{
    
    


        StatisticDo statisticDo = new StatisticDo();

        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        StringBuffer requestURL = request.getRequestURL();
        int idValue  = Integer.parseInt(request.getParameter("id"));
        HashMap map = new HashMap<String,Object>();
        String url = requestURL.toString()+idValue;
        System.out.println(url);
        map.put("url",url);
        map.put("newsId",idValue);
        HashOperations hashOperations = redisTemplate.opsForHash();
        if( hashOperations.get(url,idValue) == null) {
    
    
            if (statisticService.sqlSearch(url,idValue) == null) {
    
    
                statisticDo.setUrl(url);
                statisticDo.setNewsId(idValue);
                statisticDo.setOperateTime(new Date());
                statisticDo.setClickDate(new Date());
                statisticDo.setTimes(1);
                statisticService.save(statisticDo);
                statisticService.saveRedis(url, idValue, 1);
            } else {
    
    

                StatisticDo statisticDo1 = statisticService.sqlSearch(url, idValue);
                int times1 = statisticDo1.getTimes();
                statisticService.updateRedis(url, idValue,times1+1);
                statisticService.update(url,idValue);

            }
        }
        else {
    
    
            int times = (int)hashOperations.get(url, idValue);
            statisticService.updateRedis(url,idValue,times+1);
            System.out.println(times);
        }

      Object proceed = point.proceed();//目标方法
      return proceed;

  }
    //方法运行出现异常时调用
    @AfterThrowing(pointcut = "execution (* com.bootdo.home.controller.HomeController.shownews(..))",throwing = "ex")
    public void afterThrowing(Exception ex){
    
    
        System.out.println("aop around 方法中的错误");
        System.out.println(ex);
    }
}

增加后端点击量信息成功:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42664961/article/details/106464951