我正在尝试找到一种使用 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 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,它可以工作。