有没有办法使用Spring&使用纯HTML页面而不是使用jsp的REST?

时间:2013-09-24 19:51:17

标签: html spring spring-mvc

我正在尝试找到一种方法来配置带有REST的Spring MVC,以便使用普通的html页面。我想这样做的原因是因为我想创建一个使用手机间隙以及html和javascript的android或ios应用程序。

这将帮助我减少维护和编程,以支持浏览器和移动应用程序。到目前为止,我已经开始使用eclipse的spring mvc模板创建的基本示例项目。如果视图是jsp格式,它会很好,但是,如果我将视图更改为.html并将后缀更改为html,我也会找不到页面。

我的第二个问题,从我看过的所有例子中,他们都只使用.jsp文件。可以用jsf替换为xhtml吗?

测试的示例代码

/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping("/wb")
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /**
     * Simply selects the home view to render by returning its name.
     */

        @RequestMapping("/create/")
        public String home(Device device, Model model) {
            if (device == null) {
                logger.info("no device detected");
            } else if (device.isNormal()) {
                logger.info("Device is normal");
            } else if (device.isMobile()) {
                logger.info("Device is mobile");
            } else if (device.isTablet()) {
                logger.info("Device is tablet");
            }
            return "home";
        }
}

Beans配置

<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
        <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven>
        <argument-resolvers>
            <beans:bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
        </argument-resolvers>
    </annotation-driven>

        <!-- Use the DeviceResolverHandlerInterceptor OR the DeviceResolverRequestFilter in the web.xml -->
    <interceptors>
        <!-- On pre-handle, resolve the device that originated the web request -->
       <beans:bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
    </interceptors>
    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".html" />
    </beans:bean>

    <context:component-scan base-package="com.veera.myapp" />



</beans:beans>

使用.html时记录

INFO : com.veera.myapp.HomeController - Device is normal
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/myapp-1.0.0-BUILD-SNAPSHOT/WEB-INF/views/home.html] in DispatcherServlet with name 'appServlet'

如果我使用.jsp而不是.html,它可以工作。 enter image description here

0 个答案:

没有答案