How to include a controller from jsp page

六月ゝ 毕业季﹏ 提交于 2019-12-20 03:08:41

问题


I'm using Spring MVC.

I want to include menu page (named menu.jsp) with a controller (named MenuController) to my main page. If I call http://localhost:8080/myWeb/menu.htm everything is fine. But I try include menu into my main page like this :

<c:import url="menu.jsp"></c:import>
   Or  <c:import url="menu.htm"></c:import>

it shows nothing

This is config in dispatcher-servlet.xml file:

    <mvc:annotation-driven />

    <context:component-scan base-package="controllers" />


    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix">
            <value>/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

and web.xml

  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>

Where am I wrong? Please help me. Thanks!


回答1:


Try using <%@ include file="menu.jsp" %> or <jsp:include page="menu.jsp" />

By the way, no controller interferes here - this happens purely in the view.

On the other hand, if you want your controller to set some data required in the menu, then you should indeed use <c:import />. Not however that the path there is relative to the current page. So make sure it is correct.




回答2:


I just discovered today you can trigger the controller by referencing the relative Spring url in the include.

<jsp:include page="/ConrollerRequestMapping/your/method/request/mapping"/>


来源:https://stackoverflow.com/questions/5907170/how-to-include-a-controller-from-jsp-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!