Spring 3.2矩阵变量是什么? –第2部分:代码

关于Spring对Matrix Variables的支持,我最近的博客集中在解释它们是什么以及为什么要使用它们。 整理了内容原因之后 ,此博客全面介绍了如何以及如何使用它们。 我还给出了几个Matrix URI的示例,因此,演示一些处理几个URI的代码似乎是个好主意。

示例包括:

http://localhost:8080/spring_3_2/matrixvars/stocks;BT.A=276.70,+10.40,+3.91;AZN=236.00,+103.00,+3.29;SBRY=375.50,+7.60,+2.07

http://localhost:8080/spring_3_2/matrixvars/stocks;BT.A=276.70,+10.90,+3.91;AZN=236.00,+103.00,+3.29;SBRY=375.50,+7.60,+2.07/account;name=roger;number=105;location=stoke-on-trent,uk

如您所料,在编写处理Matrix变量的代码时,Spring专家通过引入新的@MatrixVariable注释,在现有的Spring MVC框架的基础上进行了@MatrixVariable 。 这用于注释请求处理程序方法参数,以便Spring可以注入矩阵uri的相关位。 @MatrixVariable有四个参数: valuedefaultValuepathVarrequired ,所有这些在Springs javadocs中都有详细说明。

等等,到一些代码...如果您还记得我在上一个有关此主题的博客中,我选择的场景是处理大量股价/股票的场景,而Github上提供的示例应用程序采用Matrix URI,将其砍掉并将其添加到Model以供JSP显示。

在编写代码时,首先要做的是创建一个新的控制器来处理URI……

@Controller 
@RequestMapping(value = "/matrixvars") 
public class MatrixVariableController { 
 
  private static final Logger logger = LoggerFactory.getLogger(MatrixVariableController.class); 
}

在代码中,我添加了一个类级别的@RequestMapping批注,其中包含我的URI的第一块: matrixvars 。 这是一件很有用的事情,因为它将包含值' matrixvar '的所有URI定向到此控制器的第一个路径元素,并节省了大量重复。

接下来要做的是向此类中添加一些处理第一个URI的代码:

http://localhost:8080/spring_3_2/matrixvars/stocks;BT.A=276.70,+10.40,+3.91;AZN=236.00,+103.00,+3.29;SBRY=375.50,+7.60,+2.07

第一个请求处理程序方法是:

@RequestMapping(value = "/{stocks}", method = RequestMethod.GET) 
  public String showPortfolioValues(@MatrixVariable Map<String, List<String>> matrixVars, Model model) { 
 
    logger.info("Storing {} Values which are: {}", new Object[] { matrixVars.size(), matrixVars }); 
 
    List<List<String>> outlist = map2List(matrixVars); 
    model.addAttribute("stocks", outlist); 
 
    return "stocks"; 
  } 
 
  private List<List<String>> map2List(Map<String, List<String>> stocksMap) { 
 
    List<List<String>> outlist = new ArrayList<List<String>>(); 
 
    Collection<Entry<String, List<String>>> stocksSet = stocksMap.entrySet(); 
 
    for (Entry<String, List<String>> entry : stocksSet) { 
 
      List<String> rowList = new ArrayList<String>(); 
 
      String name = entry.getKey(); 
      rowList.add(name); 
 
      List<String> stock = entry.getValue(); 
      rowList.addAll(stock); 
      outlist.add(rowList); 
    } 
 
    return outlist; 
  }

查看@RequestMapping批注,您可以看到我为其分配了/{stocks}的值。 当与类级别的@RequestMapping注释结合使用时,它将指示Spring将任何匹配的请求映射到此方法。 花括号内的文本{stocks}表示可以解析URI的这一部分并将其注入到适当的方法参数中。

接下来,看看@MatrixVariable批注。 这恰好位于我希望将股票数据注入其中的论点的前面; 但是,这里有些棘手的事情是使参数类型正确。 如果您弄错了,那么当您尝试使用数据时,您只会得到ClassCastException 。 当输入数据的格式为:

A=B,C,D

要么

A=B,C,D;W=X,Y,Z

…然后类型是Map<String,List<String>> ,其中键是AW ,它们各自的值是B,C,DX,Y,Z

因此,鉴于上述URI,map参数将包含…。

{BT.A=[276.70, +10.40, +3.91], AZN=[236.00, +103.00, +3.29], SBRY=[375.50, +7.60, +2]}

这一点很重要,该方法的其余部分非常简单,只需将输入映射转换为列表,然后将其添加到模型中,供JSP(此处未显示)显示。 请注意,这不是非常有用的代码,因此不必过多地关注它,而且我不喜欢将集合嵌入集合中-这似乎不是一个好主意。

继续,我现在看下一个URI。 请注意,我故意使它与第一个相似,唯一的区别是增加了用户的帐户详细信息:

http://localhost:8080/spring_3_2/matrixvars/stocks;BT.A=276.70,+10.90,+3.91;AZN=236.00,+103.00,+3.29;SBRY=375.50,+7.60,+2.07/account;name=roger;number=105;location=stoke-on-trent,uk

此URI映射到以下方法:

@RequestMapping(value = "/{stocks}/{account}", method = RequestMethod.GET) 
  public String showPortfolioValuesWithAccountInfo(@MatrixVariable(pathVar = "stocks") Map<String, List<String>> stocks, 
      @MatrixVariable(pathVar = "account") Map<String, List<String>> accounts, Model model) { 
 
    List<List<String>> stocksView = map2List(stocks); 
    model.addAttribute("stocks", stocksView); 
 
    List<List<String>> accountDetails = map2List(accounts); 
    model.addAttribute("accountDetails", accountDetails); 
 
    return "stocks"; 
  }

在这种情况下,完整路径描述为/matrixvars/{stocks}/{account} 。 我猜想这只是告诉Spring /matrixvars要查找/matrixvars ,后跟一个'/'后跟任何东西,然后是'/' ,后跟任何东西。

在这种情况下有两种@MatrixVariable ,而我已经添加了注解的注解pathVar参数提供值stocksaccounts 。 这些整齐地标记了矩阵变量值需要注入到方法参数中的位置。

最后要记住的一点是,矩阵变量非常灵活。 @MatrixVaraible批注还有另外三个参数,我在这里没有考虑; 但是,每种情况下的通用过程都是相同的:获取URI,找出不同的矩阵变量,设计请求处理程序,并将URI的矩阵变量映射到您的方法参数–注意确保您获得参数类型正确。

  • Github上提供了此博客的完整示例代码:https://github.com/roghughe/captaindebug/tree/master/spring-3.2

翻译自: https://www.javacodegeeks.com/2014/05/just-what-are-spring-3-2-matrix-variables-part-2-the-code.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值