IDEA,搭建spring+maven多模块,启动报错

我使用IDEA搭建spring+maven多模块项目,实现一个最简单的hello world,现在启动报错了:
The requested resource (/) is not available。
clipboard.png
项目目录结构如下:

![图片上传中...]

web.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

    <display-name>com.jd-web</display-name>

    <!-- 加载springMVC配置 -->
    <servlet>
        <servlet-name>springServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

spring-config.xml代码

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.2.xsd"
        default-autowire="byName">
        <bean class="com.jd.spring.task.common.spring.ApplicationContextUtil"/>
        <!-- 注解扫描包 -->
        <context:component-scan base-package="com.jd.spring.task.web" />
        <!-- 导入分类配置 -->
        <import resource="classpath*:spring/spring-config-mvc.xml"/>
        <import resource="classpath*:spring/spring-config-work.xml"/>
    </beans>
    
   spring-config-mvc.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                        http://www.springframework.org/schema/context   
                        http://www.springframework.org/schema/context/spring-context-3.2.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"
       default-autowire="byName">

    <!-- 添加MVC注解模式 -->
    <mvc:annotation-driven/>
    <!-- 映射静态资源 -->
    <mvc:resources location="/WEB-INF/statics/" mapping="/resource/**"/>
    <!-- 加载属性文件 -->
    <bean class="com.jd.spring.task.common.spring.CustomizedPropertyConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
    </bean>

    <!-- 视图解析器配置 -->
    <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="/WEB-INF/vm/" />
        <property name="configLocation" value="classpath:velocity.properties" />
    </bean>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="suffix" value=".vm" />
        <property name="contentType" value="text/html;charset=UTF-8" />
    </bean>
    <!-- 文件上传 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>
</beans>

IndexController.java

@Controller
@RequestMapping("/")
public class IndexController {

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
    @Authentication(type = AuthType.PUBLIC)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Hello world!");
        return "hello";
    }
}

但是工程中有IndexController,工程启动起来后应该调用IndexController中的方法:

@Controller
@RequestMapping("/")
public class IndexController {

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
    @Authentication(type = AuthType.PUBLIC)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Hello world!");
        return "hello";
    }
}
但是为什么报资源找不到?
阅读 4.3k
1 个回答
  1. 检查在你的模板目录下是否有名称为hello.vm的模板?
  2. 将@Controller改为@RestController
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题