如何通过index.html spring更改index.jsp

时间:2015-04-27 15:26:40

标签: java spring spring-mvc

我尝试使用带有angularjs的spring,所以我需要默认页面为html文件。 Here我看到可以使用mvc:resources将网址映射到目录位置,但无法使其正常工作,服务器永远不会找到该网页。如果我使用viewResolver工作完美,但这不是我的目标。

这是我的调度程序-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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.outbottle" />
    <mvc:annotation-driven />
    <mvc:resources mapping="/**" location="/html/" />

    <!--Old code-->
    <!--bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver"
              p:prefix="/WEB-INF/html/"
              p:suffix=".jsp" /-->

</beans>

我的项目结构:

enter image description here

这是应该处理默认请求的控制器

@Controller
public class DefaultController {

    @RequestMapping(value="/", method= RequestMethod.GET)
    public String index(ModelMap map) {
        map.addAttribute("hello", "Hello Spring from Netbeans!!");
        return "html/index.html";
    }

    /*Old code*/
    /*@RequestMapping(value="/", method= RequestMethod.GET)
    public String index(ModelMap map) {
        map.addAttribute("hello", "Hello Spring from Netbeans!!");
        return "index";
    }*/

}
你能告诉我我做错了什么吗?在此先感谢!!

1 个答案:

答案 0 :(得分:0)

我将html文件夹移动到Web Pages目录。

enter image description here

然后我将此行<mvc:resources mapping="/**" location="/html/" />更改为<mvc:resources mapping="/**" location="/,classpath:/html/" />,并按预期工作。谢谢大家的帮助。

相关问题