首页 新闻 赞助 找找看

关于SpringMVC项目中controller类的命名

0
悬赏园豆:10 [已解决问题] 解决于 2017-03-16 17:52

今天闲的没事,想研究了一下SpringMVC框架,就自己搭了一个,然后怎么弄也弄不通,后来发现是controller类的命名方式有问题,必须命名成 xxxController的形式,写成xxxControll就找不到了,这是什么原因呢,本人技术小白,还请大神提出高见。

坐在家里晒太阳的主页 坐在家里晒太阳 | 初学一级 | 园豆:117
提问于:2017-03-16 14:47
< >
分享
最佳答案
0

这个命名要和spring MVC的配置文件中对应的,应该是你的配置文件写死了,所有才会这样的吧

收获园豆:10
让我发会呆 | 老鸟四级 |园豆:2929 | 2017-03-16 16:01

您好,我的目录结构是这样的

然后springMVCtest-servlet.xml里就配置了两句话

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
      http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util-4.2.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-4.2.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
      http://www.springframework.org/schema/task 
      http://www.springframework.org/schema/task/spring-task.xsd ">

    <!-- 使用默认的注解映射 
    <mvc:annotation-driven />
    <mvc:resources location="/" mapping="/index.html" />
      <context:annotation-config></context:annotation-config>-->

    <!-- 自动扫描controller包中的控制器 -->
    <context:component-scan base-package="com.controller" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    <!-- <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/index.do">controllerDoem</prop>
            </props>
        </property>
    </bean>
    
     <bean id="controllerDoem" class="com.controller">
        <property name="view">
            <value>index</value>
        </property>
    </bean>-->

</beans>

也没看到哪里配置啊

坐在家里晒太阳 | 园豆:117 (初学一级) | 2017-03-16 16:16

@坐在家里晒太阳: 我只能说这和名字叫什么没关系的,只要你url映射的没问题,名字是不会影响访问的

让我发会呆 | 园豆:2929 (老鸟四级) | 2017-03-16 16:51

@让我发会呆:

访问URL应该是localhost:8080/项目名/index 吧,这个也不涉及到类名,,,哎,懵了!

坐在家里晒太阳 | 园豆:117 (初学一级) | 2017-03-16 17:19

@坐在家里晒太阳: 是的,项目名就是你web的名字,index就是你controller中@RequestMapping(value="")

这个标签的value值,所以和名字是没关系的,如果你改个名字url就访问不了了,可能是改了名字后的controller没有注入到spring,这也就是我之前的猜测

让我发会呆 | 园豆:2929 (老鸟四级) | 2017-03-16 17:33

@让我发会呆: 这次我又试了下,好像莫名其妙的好了,什么也没动,,,我发誓之前绝对不能用,不管怎么说,还是谢谢你,分给你了。

坐在家里晒太阳 | 园豆:117 (初学一级) | 2017-03-16 17:51
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册