简体   繁体   English

HTML与Spring MVC的集成

[英]Html integration with Spring MVC

I had sample poc which I did with jsp and spring mvc and working fine, I configured DispatcherServlet and InternalResourceViewResolver like this 我有使用jsp和spring mvc进行的示例poc,并且工作正常,我像这样配置了DispatcherServlet和InternalResourceViewResolver

<servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
</servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

and in my servlet-context.xml I configured InternalResourceViewResolver like this 在我的servlet-context.xml中,我像这样配置了InternalResourceViewResolver

<beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp"  />
</beans:bean>

My request and response are working good. 我的要求和回应运作良好。 Now I am trying to start a new sample project with html rather than jsp I changed InternalResourceViewResolver like this 现在,我尝试使用html而不是jsp启动一个新的示例项目,我这样更改了InternalResourceViewResolver

class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <beans:property name="prefix" value="/WEB-INF/views/" />
            <beans:property name="suffix" value=".html"  />
</beans:bean>

but I am getting an exception that 但我得到一个例外

"Info: WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/Organization_Management/WEB-INF/views/check.html] in DispatcherServlet with name 'appServlet'" “信息:警告:org.springframework.web.servlet.PageNotFound-在名称为'appServlet'的DispatcherServlet中找不到带有URI [/Organization_Management/WEB-INF/views/check.html]的HTTP请求的映射”

I want to start a new sample application with html and spring mvc. 我想用html和spring mvc启动一个新的示例应用程序。 can any one please suggest me in this regard. 有人可以在这方面建议我。

Solution 1: In the InternalResourceViewResolver you can leave the Suffix part empty and the InternalResourceViewResolver will resolve both .jsp as well as .html files. 解决方案1:InternalResourceViewResolver中 ,可以将后缀部分保留为 ,而InternalResourceViewResolver将同时解析.jsp和.html文件。

But make sure that in your controller you have have methods that return html views and methods that return jsp views based on the suffix. 但是请确保您的控制器中具有返回html视图的方法和基于后缀返回jsp视图的方法。 For example, if index.html and index.jsp both exist in WEB-INF/pages you can do: 例如,如果WEB-INF / pages中都存在index.html和index.jsp,则可以执行以下操作:

@RequestMapping("/htmlView")
   public String renderHtmlView() {
      return "index.html";
}

@RequestMapping("/jspView")
   public String renderJspView() {
      return "index.jsp";
}

Solution 2 : Since .html files are static and do not require processing by a servlet then it is more efficient, and simpler, to use an mapping. 解决方案2 :由于.html文件是静态的,不需要servlet进行处理,因此使用映射更加有效和简单。 This requires Spring 3.0.4+. 这需要Spring 3.0.4+。

For example: 例如:

<mvc:resources mapping="/static/**" location="/static/" />

which would pass through all requests starting with /static/ to the webapp/static/ directory. 它将通过所有以/ static /开头的请求传递到webapp / static /目录。

So by putting index.html in webapp/static/ and using return "static/index.html"; 因此,将index.html放在webapp / static /中并使用return“ static / index.html”; from your method, Spring should find the view. 从您的方法中,Spring应该找到视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM