3

I have a web app and I want add the mobile version to it..

So I followed this guide to add spring-mobile, but I can't get my mobile views..

I don't want add in every controller's methods this piece of code:

if (device.isMobile()) {
  return "mobile/myPage.jspx";
} else if (device.isTablet()) {
  return "tablet/myPage.jspx";
} else {
  return "myPage.jspx";
}

So I'm trying to set a view-resolver to get the right page. I use Tiles and this is its configuration:

<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="tilesViewResolver">
  <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>

And then I add this:

<bean class="org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver">
  <constructor-arg>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/views/" />
      <property name="suffix" value=".jsp" />
    </bean>
  </constructor-arg>
  <property name="tabletPrefix" value="tablet/" />
</bean>

But my we-app returns only /myPage.jspx and never /mobile or /tablet pages.

How can I do?

Thank you!

hsz
  • 148,279
  • 62
  • 259
  • 315
Teo
  • 3,143
  • 2
  • 29
  • 59

1 Answers1

4

That isn't going to work. The UrlBasedViewResolver always returns a view regardless the fact if it exists or not. Also your UrlBasedViewResolver is always consulted first basically rendering your LiteDeviceDelegatingViewResolver useless.

You also have to let your mobile views use Tiles and make sure that the configured prefix leads to a modified view. I would also suggest to use the TilesViewResolver convenience subclass, save you some XML.

<bean class="org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver">
    <constructor-arg>
        <bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver" />
    </constructor-arg>
    <property name="mobilePrefix" value="mobile/" />
    <property name="tabletPrefix" value="tablet/" />
</bean>

And ofcourse remove your other configured ViewResolver.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • I used your code, but I obtain this: Could not resolve view with name 'login' in servlet with name 'appServlet' but in view.xml I added the /tablet/login.jspx.. – Teo Jun 11 '14 at 08:50
  • Do you actually have a Tiles view named `login`? THe name is `tablet/login.jspx` not `/tablet/login.jspx`. – M. Deinum Jun 11 '14 at 08:51
  • Yes I have it: one login page in `/WEB-INF/views/login.jspx` and the other one is in `/WEB-INF/views/tablet/login.jspx` and both with their `view.xml` file – Teo Jun 11 '14 at 08:59
  • It is not the actual location it is the name it needs to resolve to (at least what I remember from tiles). The name isn't the same (generally) as the location of the template/view. – M. Deinum Jun 11 '14 at 09:04
  • Ok, I solved it.. I writed the wrong name of the view in my .xml file.. Thank you! – Teo Jun 12 '14 at 06:02