我正在尝试制作一个Boot / MVC项目。当我启动Tomcat时,页面显示完美,但是当我尝试使用ModelAndView
对象添加任何内容时,页面上没有任何内容。控制台上甚至不显示System.out.println("anything")
。
这是Application.java
package com.markham.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
IndexController.java:
package com.markham.main;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.TypedQuery;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@RestController
public class IndexController {
@PersistenceContext(type = PersistenceContextType.TRANSACTION)
private EntityManager em;
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView index() {
ModelAndView view = new ModelAndView();
List<User> users = new ArrayList<User>();
TypedQuery<User> query1 = em.createNamedQuery("User.getAllUsers", User.class);
users = query1.getResultList();
System.out.println("HERE'S THE THING---" + users); //This doesn't even print
view.addObject("users", users);
return view;
}
}
我怎样才能使我的控制器与我的index.jsp一起使用 - 默认页面?
如果您需要更多代码,请询问,我会提供。
编辑:
的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>guru.springframework</groupId>
<artifactId>spring-boot-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spring Boot Web Application</name>
<description>Spring Boot Web Application</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties:
spring.datasource.url = jdbc:mysql://example
spring.datasource.username = example
spring.datasource.password = example
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
# Show or not log for each sql query
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
编辑#2:这是我访问http://localhost:8080/BootApp/
以下是日志:
Mar 16, 2016 6:13:57 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:BootApp' did not find a matching property.
Mar 16, 2016 6:13:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/8.0.26
Mar 16, 2016 6:13:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Aug 18 2015 11:38:37 UTC
Mar 16, 2016 6:13:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 8.0.26.0
Mar 16, 2016 6:13:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Mac OS X
Mar 16, 2016 6:13:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 10.11.3
Mar 16, 2016 6:13:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: x86_64
Mar 16, 2016 6:13:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre
Mar 16, 2016 6:13:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_60-b27
Mar 16, 2016 6:13:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Mar 16, 2016 6:13:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: /Users/ben/Documents/workspace/rabbitmq/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Mar 16, 2016 6:13:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: /Users/ben/Downloads/apache-tomcat-8.0.26
Mar 16, 2016 6:13:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/Users/ben/Documents/workspace/rabbitmq/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Mar 16, 2016 6:13:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/Users/ben/Downloads/apache-tomcat-8.0.26
Mar 16, 2016 6:13:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=/Users/ben/Documents/workspace/rabbitmq/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
Mar 16, 2016 6:13:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/Users/ben/Downloads/apache-tomcat-8.0.26/endorsed
Mar 16, 2016 6:13:57 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Mar 16, 2016 6:13:57 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /Users/ben/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
Mar 16, 2016 6:13:57 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Mar 16, 2016 6:13:57 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Mar 16, 2016 6:13:57 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Mar 16, 2016 6:13:57 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Mar 16, 2016 6:13:57 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 891 ms
Mar 16, 2016 6:13:57 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 16, 2016 6:13:57 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.26
Mar 16, 2016 6:13:57 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
WARNING: Name = samsung Property maxActive is not used in DBCP2, use maxTotal instead. maxTotal default value is 8. You have set value of "-1" for "maxActive" property, which is being ignored.
Mar 16, 2016 6:13:57 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
WARNING: Name = samsung Property maxWait is not used in DBCP2 , use maxWaitMillis instead. maxWaitMillis default value is -1. You have set value of "-1" for "maxWait" property, which is being ignored.
Mar 16, 2016 6:13:57 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
INFO: Name = samsung Ignoring unknown property: value of "true" for "spring.datasource.testOnBorrow" property
Mar 16, 2016 6:13:57 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
INFO: Name = samsung Ignoring unknown property: value of "SELECT 1" for "spring.datasource.validationQuery" property
Mar 16, 2016 6:13:57 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
INFO: Name = samsung Ignoring unknown property: value of "true" for "autoReconnect" property
Mar 16, 2016 6:13:57 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Mar 16, 2016 6:14:00 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
WARNING: Name = samsung Property maxActive is not used in DBCP2, use maxTotal instead. maxTotal default value is 8. You have set value of "-1" for "maxActive" property, which is being ignored.
Mar 16, 2016 6:14:00 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
WARNING: Name = samsung Property maxWait is not used in DBCP2 , use maxWaitMillis instead. maxWaitMillis default value is -1. You have set value of "-1" for "maxWait" property, which is being ignored.
Mar 16, 2016 6:14:00 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
INFO: Name = samsung Ignoring unknown property: value of "true" for "spring.datasource.testOnBorrow" property
Mar 16, 2016 6:14:00 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
INFO: Name = samsung Ignoring unknown property: value of "SELECT 1" for "spring.datasource.validationQuery" property
Mar 16, 2016 6:14:00 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
INFO: Name = samsung Ignoring unknown property: value of "true" for "autoReconnect" property
Mar 16, 2016 6:14:01 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Mar 16, 2016 6:14:01 PM org.apache.catalina.core.ApplicationContext log
INFO: Spring WebApplicationInitializers detected on classpath: [org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration$JerseyWebApplicationInitializer@60111f5c]
Mar 16, 2016 6:14:01 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Mar 16, 2016 6:14:01 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Mar 16, 2016 6:14:01 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 3713 ms
编辑#3(感谢所有人的帮助):执行mvn spring-boot:run
后,会出现此错误:
[ERROR] No plugin found for prefix 'spring-boot' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/Users/ben/.m2/repository), central (https://repo.maven.apache.org/maven2)] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundForPrefixException
答案 0 :(得分:1)
您的ModelAndView
并未说明要访问的文件
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView index() {
ModelAndView view = new ModelAndView("index"); // This line
List<User> users = new ArrayList<User>();
TypedQuery<User> query1 = em.createNamedQuery("User.getAllUsers", User.class);
users = query1.getResultList();
System.out.println("HERE'S THE THING---" + users); //This doesn't even print
view.addObject("users", users);
return view;
}
您的application.properties
没有MVC View Resolver配置。
spring.mvc.view.prefix=<Path To Jsp>
spring.mvc.view.suffix=.jsp
首先进行这些更改。
答案 1 :(得分:1)
问题:您正在尝试在外部服务器中运行SpringBoot应用程序,而不是在嵌入式tomcat中运行。
在嵌入式容器中作为spring boot运行:
要作为spring boot运行,请在包含pom.xml的目录中使用mvn spring-boot:run
命令。我建议你通过https://spring.io/guides/gs/serving-web-content/入门指南。
部署到外部服务器 如果要部署在外部服务器中,请将打包从jar更改为war并部署war文件。部署到WAS的参考https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html
答案 2 :(得分:1)
在IndexController.class
中使用@RestController
更改@Controller
并添加wiew page mame ModelAndView
object
ModelAndView view = new ModelAndView("index.jsp");
和index.jsp spring boot default location src/main/webapp
答案 3 :(得分:0)
如果要将请求重定向到特定页面。在创建 ModelAndView 对象时,您需要将此页面名称作为参数传递。
示例:如果您要将请求重定向到welcome.jsp
页面,则可以创建 ModelAndView ,如下所示:
ModelAndView view = new ModelAndView("welcome.jsp");
//code as per your requirement
return view;
您需要在 pom.xml 文件中使用以下依赖项。
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
注意:我假设您的welcome.jsp
页面位于以下包中:
src/main/webapp
您可以找到简单的演示应用程序here。
答案 4 :(得分:0)
好吧,所以我终于得到了它的工作,感谢几乎所有帮助过的人。
当我没有将它作为Spring Boot应用程序运行时,@ BalajiKrishnan是正确的。我将它从jar更改为war并使用mvn spring-boot:run
,现在它作为Spring Boot应用程序运行。 BalajiKrishnan's Post
然而,我仍然得到404 errors
并使用@shazin的答案来改变我发布的application.properties
方式。 here
仍在获得404 errors
所以我将我的网址从localhost:8080/BootApp/
更改为localhost:8080
,就像评论中建议的@EpicPandaForce一样。
我改变了我使用ModelAndView view = new ModelAndView("index");
,就像@shazin建议的那样。
感谢所有贡献的人!