Spring返回字符串但必须返回html

@RestController

public class ControllerCustomer {

    @RequestMapping("customer")

    public String customer(){

        return "customer";

    }

模板文件夹中的文件 customer.html

http://img2.mukewang.com/619e1638000118b811500643.jpg

Helenr
浏览 171回答 2
2回答

凤凰求蛊

如果要使用jsp,则创建一个文件夹main ->&nbsp;&nbsp; &nbsp; &nbsp; webapp ->&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; WEB-INF->&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; views将您的 jsp 页面放在views文件夹中。现在在你application.properties添加这些行spring.mvc.view.prefix:/WEB-INF/views/spring.mvc.view.suffix:.jsp现在在你pom.xml添加这些依赖<dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>jstl</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>jstl</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.tomcat.embed</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>tomcat-embed-jasper</artifactId></dependency>

函数式编程

您是否配置了 Spring MVC?通过在 spring 配置文件中添加以下内容来使用 Spring MVC InternalResourceViewResolver:<bean id="viewResolver"&nbsp; &nbsp; class="org.springframework.web.servlet.view.InternalResourceViewResolver">&nbsp; &nbsp; <property name="prefix" value="/WEB-INF/pages/"></property>&nbsp; &nbsp; <property name="suffix" value=".html"></property>&nbsp;</bean>并且只在控制器中返回“客户”在您的注释驱动配置中,应在扩展 WebMvcConfigurerAdapter 的配置类中设置 InternalResourceViewResolver,如下所示:@Beanpublic ViewResolver getViewResolver(){&nbsp; &nbsp; InternalResourceViewResolver resolver = new InternalResourceViewResolver();&nbsp; &nbsp; resolver.setPrefix("/WEB-INF/pages/");&nbsp; &nbsp; resolver.setSuffix(".html");&nbsp; &nbsp; return resolver;}这两行将发挥所有作用。&nbsp; resolver.setPrefix("/WEB-INF/pages/");&nbsp; resolver.setSuffix(".html");它将客户设置为 customer.html,您的页面应该在 /WEB-INF/pages/ 中。如果你使用 Spring boot 在 application.properties 中添加这两个属性,&nbsp; spring.mvc.view.prefix=/view/&nbsp; spring.mvc.view.suffix=.jsp并添加这两个依赖项。&nbsp;<dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf</artifactId>&nbsp;</dependency>&nbsp;<dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf-spring4</artifactId></dependency>名为 customer.html 的视图放置在 src/main/resources/templates 中。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java