springMVC 注解和非注解同时用,注解模式报404错误

一、问题描述:
单独用注解模式或者非注解模式都可以正常调用。但是注解和非注解一起用的时候会出现注解模式报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;
    }

}

二、写在最后
现在还没有答案,还不明白为啥

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值