好的,
我试图使一个简单的mvcSpring启动应用程序,我有它在我的代码返回index.html当控制器接收到"/"的请求。
我不确定,但这不起作用。
SpringDataWebApplication。JAVA
package com.demo.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@SpringBootApplication
@EnableAutoConfiguration
@EnableJpaRepositories("com.demo.repositories.*")
@EntityScan("com.demo.entities.*")
@ComponentScan(basePackages = {"com.demo.controller.*","com.demo.service.*"})
public class SpringDataWebApplication {
//TODO : Logging
public static void main(String[] args) {
SpringApplication.run(SpringDataWebApplication.class, args);
}
}
HealthCHeckController。JAVA
package com.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HealthCheckController {
@RequestMapping("/")
public String getSystemHealth() {
return "index";
}
}
用户存储库。JAVA
package com.demo.repositories;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.demo.entities.UserEntity;
@Repository
public interface UserRepository extends CrudRepository<UserEntity, String> {
}
指数html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>BlogPost URL check</title>
</head>
<body>
<h1>HealthCHeck Working as desired</h1>
</body>
</html>
指数html位于/templates目录中,如thymeleaf所示
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.demo</groupId>
<artifactId>Example1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>0.7.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
用户实体。java:这还不是必需的,因为我正在一步一步地进行,我只希望得到请求“/”的响应,但我无法得到响应。
package com.demo.entities;
import java.io.Serializable;
import java.util.Calendar;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
@Entity
@Table(name="user")
public class UserEntity implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String Id;
private String firstName;
private String lastName;
private String address1;
private String address2;
private String city;
private String state;
private String zipcode;
private Calendar birthDate;
private String gender;
private String email;
private String verifyEmail;
private String password;
private String verifyPassword;
private String createdBy;
private String modifiedBy;
private Calendar createdAt;
private Calendar modifiedAt;
public UserEntity() {
}
@GeneratedValue(strategy=GenerationType.SEQUENCE)
@NotNull
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress1() {
return address1;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public Calendar getBirthDate() {
return birthDate;
}
public void setBirthDate(Calendar birthDate) {
this.birthDate = birthDate;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@javax.persistence.Id
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getVerifyEmail() {
return verifyEmail;
}
public void setVerifyEmail(String verifyEmail) {
this.verifyEmail = verifyEmail;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getVerifyPassword() {
return verifyPassword;
}
public void setVerifyPassword(String verifyPassword) {
this.verifyPassword = verifyPassword;
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public String getModifiedBy() {
return modifiedBy;
}
public void setModifiedBy(String modifiedBy) {
this.modifiedBy = modifiedBy;
}
public Calendar getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Calendar createdAt) {
this.createdAt = createdAt;
}
public Calendar getModifiedAt() {
return modifiedAt;
}
public void setModifiedAt(Calendar modifiedAt) {
this.modifiedAt = modifiedAt;
}
}
你一定要读到:
http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#boot-以spring mvc静态内容为特色
你试图点击一个简单的HTML页面,它实际上是一个静态内容。从Spring Boot文档:
默认情况下,Spring Boot将从类路径中名为/static(或/public或/resources或/META-INF/resources)的目录或ServletContext的根目录提供静态内容。
首先,这意味着您需要确保index.html页面位于静态文件夹下,Spring Boot中的静态文件夹位于src/main/资源下。
第二,当你试图在spring boot中提供静态内容时,你需要并且想要破坏缓存,这意味着你的浏览器正在缓存你的旧请求,为了看到你的更改正在发生,你需要将它添加到你的应用程序中。属性文件:
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
看看这里:
Spring Boot无法从用户文件夹加载css
问题内容: 我有以下简单的看法。为什么会导致此错误? 问题答案: 因为视图必须 返回 ,而不仅仅是调用它。将最后一行更改为
我正在使用Spring boot来渲染页面。我也使用了叶子。但是我在从浏览器调用它时得到了下面提供的异常。 我已经编写了视图解析器类,当我使用为此。 和我在应用程序中提到的一样。yml也是。应用yml是 这是我的Controller类,负责渲染index.jspMainController.class
问题内容: 错误信息: 而我的代码: 并且my-form.html是: 如果我在其中放置一个while循环,它将永远加载。 然后输入比当前页面永久加载的更高的温度。如果我使用上面列出的当前代码,则会出现错误。 问题答案: 以下内容不返回响应: 你必须返回类似或的内容。 这样可以解决问题
我正在尝试使用Laravel默认邮件外观发送电子邮件:- 在Mail\NotifyUserExpiring中:- 在网络上。邮政NotifyUserExpiring视图文件我正在使用刀片打印方式变量:- 当我检查我的邮件收件箱时,我收到了一封带有{{$user的电子邮件- 你好{{$user-
问题内容: 我有以下控制器… 我的servlet上下文使用以下值设置… 现在,如果我转到那,我得到的视图就是userManagement.jsp,这正是我想要的… 但是,如果我转到404错误。 我看不到的是为什么会发生这种情况,因为我将requestMapping设置为与上面完全相同,并且在/ WEB-INF / views中我有一个createUser.jsp和userManagement.js
在其他回收器视图中有一个回收器视图。两者都需要垂直滚动。外部回收器视图滚动正常,但内部回收器视图滚动不正常。 这是代码: ViewAdapter如下: 我尝试了以下两种回收商观点,但都无法解决问题 也尝试了这个: