有没有使用普通的jsp文件,而没有控制器与使用Spring Boot / MVC中的控制器的百万美元视图混合?
例如,标准控制器如下;
@RequestMapping("/")
public String get() {
return "view";
}
哪个工作完全符合预期,但是可以将放在文件夹中的“.jsp”文件映射到任何网址,例如test.jsp将转到www.website.com/test.jsp
问候。
答案 0 :(得分:2)
我在servlet-context.xml
中使用以下配置(DispatcherServlet的contextConfigLocation):
<!-- 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=".jsp" />
<beans:property name="viewNames" value="jsp/*"></beans:property>
<beans:property name="order" value="1"></beans:property>
</beans:bean>
<!-- Thymeleaf -->
<beans:bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<beans:property name="templateEngine" ref="templateEngine" />
<!-- This narrows the scope of the resolver to the view names (returned
by the controllers' methods) to those matching the pattern -->
<beans:property name="viewNames" value="templates/*"></beans:property>
<!-- places this resolver before the default InternalResourceViewResolver -->
<beans:property name="order" value="0"></beans:property>
</beans:bean>
<beans:bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="templateMode" value="HTML5" />
<beans:property name="suffix" value=".html" />
</beans:bean>
<beans:bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<beans:property name="templateResolver" ref="templateResolver" />
</beans:bean>
<!-- Thymeleaf _ end -->
为了使用JSP呈现响应,在控制器中我返回如下字符串:
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
return "jsp/home";
}
然后使用百日咳模板进行渲染,我会执行以下操作:
@RequestMapping(value = "/", method = RequestMethod.GET)
public String getRanks(Model model, HttpServletRequest request)
{
return "templates/user/ranks";
}
当然,.jsp
和.html
文件的物理位置必须与控制器返回的视图名称匹配。因此,根据解析器的配置方式,您必须将.jsp
个/views/jsp
文件放在.html
/views/templates
和www.website.com/test.jsp
文件中
如果您希望使用test.jsp
视图呈现web.xml
之类的网址,则只需将aformentiond jsp文件放在网络应用的根目录中即可。如果没有奇怪的配置,则应该由JSP servlet解析该请求。实际上,像Tomcat这样的servlet容器通常在其全局<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>fork</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>xpoweredBy</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
文件中具有此规范:
<!-- The mappings for the JSP servlet -->
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>
和
return $http.get(...)
如果您想要更具个性化和特定性的内容,则必须相应地修改配置。例如,从servlet容器的web.xml中删除JSP servlet,并在适当的控制器中映射* .jsp url