一、问题描述:
单独用注解模式或者非注解模式都可以正常调用。但是注解和非注解一起用的时候会出现注解模式报404错误。配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
<!--注解方法
1、单独加载或者扫描的方式加载
-->
<context:component-scan base-package="com.ssm.controller"></context:component-scan>
<!--配置适配器,映射器驱动-->
<tx:annotation-driven></tx:annotation-driven>
<!--配置handler-->
<bean id="controller1" name="/a.action" class="com.ssm.controller.ItemController1"></bean>
<bean id="controller2" class="com.ssm.controller.ItemController2"></bean>
<!--处理器映射器-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!--第二个处理器映射器-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/a1.action">controller1</prop>
<prop key="/a2.action">controller1</prop>
<prop key="/a3.action">controller2</prop>
</props>
</property>
</bean>
<!--处理器适配器
1、通过是否实现HandlerAdapter来判断是否是是配器
2、通过ublic boolean supports(Object handler) {
return (handler instanceof Controller);
}查看支持哪种handler
-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!--第二个adapter-->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>
<!--视图解析器解析jsp视图 默认使用jstl标签-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</beans>
注解模式controller3的Java类,代码如下
package com.ssm.controller;
import com.ssm.po.Item;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.List;
@Controller
public class ItemController3 {
@RequestMapping("/a4")
public ModelAndView queryItemsByAnotation(){
//声明一个假数据
List<Item> itemList = new ArrayList<Item>();
//声明第一个Item
Item item1 = new Item();
item1.setId("1");
item1.setJs("解释1");
item1.setMc("铅笔");
//声明第二个item
Item item2 = new Item();
item2.setId("2");
item2.setJs("解释2");
item2.setMc("橡皮");
//添加到list
itemList.add(item1);
itemList.add(item2);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemlistKey",itemList);
modelAndView.setViewName("/jsp/items/itemsList.jsp");
return modelAndView;
}
}
二、写在最后
现在还没有答案,还不明白为啥