获取地址栏参数

本文介绍了两种方法来获取地址栏中的参数。第一种方法是使用JavaScript,通过URLSearchParams接口解析URL查询字符串。第二种方法是在Vue应用中,通过`main.js`文件设置Vue实例的路由守卫来获取参数。

第一种:

功能:获取地址栏参数
使用:getQueryString('str')
代码:this.$getQueryString("order_id");//001002
代码:this.$getQueryString("order_title");//123

http://localhost:9010/index.html?order_id=001002&order_title=123&total_amount=1.99
*/
js:

function getQueryString(name) {
  var search = document.location.href;
  var pattern = new RegExp("[?&]" + name + "\=([^&]+)", "g");
  var matcher = pattern.exec(search);
  var items = null;
  if (null != matcher) {
    try {
      items = decodeURIComponent(decodeURIComponent(matcher[1]));
    } catch (e) {
      try {
        items = decodeURIComponent(matcher[
给定的引用中未提及使用AOP获取地址栏参数的方法。不过,通常在Spring框架里,可以借助以下步骤运用AOP获取地址栏参数: 1. **定义切面类**:创建一个带有`@Aspect`注解的类,用`@Before`注解来定义一个前置通知。 2. **使用`JoinPoint`获取方法参数**:在前置通知方法中,使用`JoinPoint`对象获取被拦截方法的参数。 3. **解析地址栏参数**:若被拦截的方法接收`HttpServletRequest`对象,可通过该对象解析地址栏参数。 以下是一个示例代码: ```java import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import java.util.Enumeration; @Aspect @Component public class UrlParameterAspect { @Before("execution(* com.example.controller.*.*(..)) && args(..,request)") public void before(JoinPoint joinPoint, HttpServletRequest request) { // 获取地址栏参数 Enumeration<String> parameterNames = request.getParameterNames(); while (parameterNames.hasMoreElements()) { String paramName = parameterNames.nextElement(); String paramValue = request.getParameter(paramName); System.out.println("参数名: " + paramName + ", 参数值: " + paramValue); } } } ``` 在上述代码中,`@Before`注解定义了一个前置通知,切入点表达式`execution(* com.example.controller.*.*(..)) && args(..,request)`表示拦截`com.example.controller`包下的所有方法,并且这些方法的最后一个参数为`HttpServletRequest`对象。在`before`方法中,通过`HttpServletRequest`对象获取地址栏参数并打印。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值