springboot与返回视图

1.使用jsp返回

application.properties
############## OAuth ################
server.port=8082
#jsp path
spring.view.prefix=/WEB-INF/page/
spring.view.suffix=.jsp

依赖包

<dependency>
			<groupId>apache-taglibs</groupId>
			<artifactId>jstl</artifactId>
			<version>1.1.2</version>
		</dependency>
		<!-- jsp解析器 -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
		</dependency>

启动方法

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RestController;

//@SpringBootApplication
@PropertySource("classpath:application.properties")
// @EnableTransactionManagement(proxyTargetClass = true)
@ImportResource({ "classpath*:springContext.xml" })
@ComponentScan("com.zy")
// @EnableAutoConfiguration
@RestController
@EnableAutoConfiguration
public class RumTimeBox {

	public static void main(String[] args) {
		SpringApplication.run(RumTimeBox.class, args);
		System.out.println("success!");
	}

	protected SpringApplicationBuilder config(SpringApplicationBuilder applicationBuilder) {
		return applicationBuilder.sources(RumTimeBox.class);
	}

}

2.springboot 与freemarker

配置文件application.properties

############## OAuth ################
server.port=8082
#jsp path
#spring.view.prefix=/WEB-INF/page/
#spring.view.suffix=.html
spring.freemarker.checkTemplateLocation=false
spring.freemarker.suffix=.html
spring.freemarker.template-loader-path=/WEB-INF/page/

依赖包

<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
			<version>2.3.25-incubating</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-freemarker</artifactId>
		</dependency>

启动方法

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RestController;

//@SpringBootApplication
@PropertySource("classpath:application.properties")
// @EnableTransactionManagement(proxyTargetClass = true)
@ImportResource({ "classpath*:springContext.xml" })
@ComponentScan("com.zy")
// @EnableAutoConfiguration
@RestController
@EnableAutoConfiguration
public class RumTimeBox extends SpringBootServletInitializer {
	/**
	 * springboot默认启动
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		SpringApplication.run(RumTimeBox.class, args);
		System.out.println("success!");
	}

	/**
	 * 重写启动方法,用于使用第三方配置如jetty 启动时使用
	 */
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(RumTimeBox.class);
	}

}

springboot配置文件中只有配置注解扫描<context:component-scan base-package="com.zy" />

Spring Boot + JPA (Java Persistence API) 的组合通常用于简化企业级应用的数据访问层。当你使用 Spring Data JPA 作为数据访问技术,并配置了 Thymeleaf 或其他模板引擎作为视图层,Spring Boot 可以帮助你自动化一些视图相关的设置。 当你有以下几个步骤: 1. 配置数据源:在 `application.properties` 或 `application.yml` 中配置数据库连接信息。 2. 添加依赖:在 Maven 或 Gradle 项目的构建文件中添加 Spring Data JPA 和相关模板引擎(如 Thymeleaf)的依赖。 ```xml <!-- Maven --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- Gradle --> implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' ``` 3. 定义实体(Entity)和 Repository:按照 JPA 规范创建实体类表示数据库表,Repository 接口用于操作数据库。 4. 创建 Dao 和 Service:如果需要,可以编写 Dao 接口(JpaRepository、CrudRepository 等)和 Service 类处理业务逻辑。 5. 配置 Thymeleaf:在 Spring Boot 的配置中启用 Thymeleaf 模板引擎,并指定模板的位置。 ```java @EnableWebMvc public class WebConfig extends WebMvcConfigurer { // ... @Override public void configureViewResolvers(ViewResolverRegistry registry) { registry.jsp(); registry.thymeleaf().templateEngine(new ThymeleafEngine()) .prefix("/templates/") .suffix(".html"); } ``` 6. 编写视图控制器:通过 RestController 或 Controller 注解处理 HTTP 请求,返回 Thymeleaf 模板渲染后的响应。 当以上配置完成,Spring Boot 会自动扫描到 Thymeleaf 模板并将其映射到对应的 REST API 路径。你可以在 Controller 中直接返回 Entity 对象,Spring Data JPA 将负责持久化和查询操作,而 Thymeleaf 会动态地渲染模板内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值