我正在学习springMVC
并遇到一个问题,即我无法使用矩阵变量,因为当我调用url
时它既没有击中controller
也没有显示任何异常基本上它不是触发预期的代码,因为我有以下类:
控制器类
@Autowired
ProductRepositiry productRepositiry;
@RequestMapping("/filter/{path}")
public String getFiltered(@MatrixVariable(pathVar = "path") Map<String, List<String>> filterParms, Model model) {
model.addAttribute("items", productRepositiry.getFilteredProducts(filterParms));
return "viewAll";
}
Repositiry Class
private List<Item> list = new ArrayList<>();
public List<Item> getList() {
Item first = new Item();
first.setItemOne("firstOne");
first.setItemTwo("firstTwo");
first.setItemThree("firstThree");
Item second = new Item();
second.setItemOne("seconeOne");
second.setItemTwo("firstTwo");
second.setItemThree("secondThree");
Item third = new Item();
third.setItemOne("thirdOne");
third.setItemTwo("thirdTwo");
third.setItemThree("thirdThree");
Item four = new Item();
four.setItemOne("fourOne");
four.setItemTwo("fourTwo");
four.setItemThree("thirdThree");
list.add(first);
list.add(second);
list.add(third);
list.add(four);
return list;
}
public Set<Item> getFilteredProducts(Map<String, List<String>> filterParms) {
Set<String> keys = filterParms.keySet();
Set<Item> itemOne = new HashSet<>();
Set<Item> itemTwo = new HashSet<>();
if (keys.contains("item")) {
for (String one : filterParms.get("item")) {
for (Item i : list) {
if (one.equalsIgnoreCase(i.getItemTwo()))
itemOne.add(i);
}
}
} else
throw new NullPointerException("value not found");
if (keys.contains("one")) {
for (String two : filterParms.get("one")) {
for (Item i : list) {
if (two.equalsIgnoreCase(i.getItemThree())) {
itemTwo.add(i);
}
}
}
}
itemOne.addAll(itemTwo);
for (Item i : itemOne) {
System.out.println(i.getItemOne());
}
return itemOne;
}
}
网址
http://localhost:8080/Matrix_Variables/filter;item=firstTwo
虽然点击此URL
viewAll
(JSP页面)应该显示来自first
类的second
和Repositiry
的数据,但它返回错误< / p>
错误
HTTP Status 404 -
type Status report
message
description The requested resource is not available.
Apache Tomcat/8.0.36
我想提一提的是,当我用("/")
运行这个项目时,它运行正常,但是当我引入矩阵变量时,它无效。
和 dispatcherServlet-servlet.xml
<mvc:annotation-driven enable-matrix-variables="true" />
<context:component-scan base-package="com" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp" />
<property name="prefix" value="/view/" />
</bean>
我想要的只是使用矩阵变量从repositiry中获取值来查看,但它没有发生,我错了,但无法找到它,你能帮我解决这个问题。
最好的问候
答案 0 :(得分:0)
您正在使用&#34;;&#34;在请求URL中。使用&#34; /&#34;代替。