1

I'm trying to use spring with angularjs, so i need default page to be a html file. Here i saw that can map an url to a directory location using mvc:resources but can't made it works, server never finds the page. If i use viewResolver works perfect, but this is not my goal.

This is my dispatcher-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>

My project structure:

enter image description here

And this is the controller that should handle the default request:

@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";
    }*/

}

Can you please tell me what am i doing wrong? Thanks in advance!!

Community
  • 1
  • 1
Aramillo
  • 3,176
  • 3
  • 24
  • 49
  • If Server cannot find the page, then its definitely a resource path issue. Look at the URL path in the browser. Play with it and try to adjust the return value of index method. – vinay Apr 27 '15 at 15:33
  • @vinay The url path is `http://localhost:8084/HelloSpring/`, so it should render according to controller `html/index.html` page, am i right? – Aramillo Apr 27 '15 at 15:36
  • Try to move your resources out of WEB-INF directory. – Laszlo Lugosi Apr 27 '15 at 15:36
  • @LaszloLugosi I'm getting the same error – Aramillo Apr 27 '15 at 15:39
  • Why do you need an index.html instead of a index.jsp ? You can use the .jsp and it will also work. AngularJS is on the client side, it does not matter and does not know if your original template is a jsp or html file – Steph Apr 27 '15 at 15:39
  • @Steph I'm not very familiar with jsp, but i think you need something else that a browser to run this files, maybe i'm wrong. I want to separate client from server completly using rest services. – Aramillo Apr 27 '15 at 15:45
  • If you make only a static site without a web container, just html/css/js yes you can't use jsp files. But as you are using spring mvc, if you want to code html, css, js, you can put it in your index.jsp file. If you don't want to use jsp files, you can use spring boot with thymleaf templating engine. See examples of Spring boot + AngularJS here: https://spring.io/blog/2015/01/12/spring-and-angular-js-a-secure-single-page-application – Steph Apr 27 '15 at 15:51

1 Answers1

0

I moved the html folder to Web Pages directory.

enter image description here

Then i changed this line <mvc:resources mapping="/**" location="/html/" /> by this one <mvc:resources mapping="/**" location="/,classpath:/html/" /> and is working as expected. Thanks all for help.

Aramillo
  • 3,176
  • 3
  • 24
  • 49
  • Good that you have it working. Is the DefaultController actually used? – vinay Apr 27 '15 at 19:31
  • Yes @vinay, it loads `html/index.html` as home page using the default controller. – Aramillo Apr 27 '15 at 19:49
  • 1
    I don't really think it is using the DefaultController. Try debugging/removing DefaultController. – vinay Apr 27 '15 at 20:52
  • Yes @vinay , i try debug the code and it is using the DefaultController, why do you think it does not? – Aramillo Apr 28 '15 at 17:57
  • 1
    Because you are using mvc:resources tag. AFAIK the tag takes care of mapping requests. I might be wrong. you might just want to check. – vinay Apr 29 '15 at 17:19
  • @vinay I check it again, but removing it this time and yes, you are right, the `DefaultController` it's not necessary. Thanks for advice :). – Aramillo Apr 29 '15 at 20:21
  • I am glad, we understand it better now. – vinay Apr 29 '15 at 21:08