【发布时间】:2016-07-02 00:23:45
【问题描述】:
我正在尝试制作一个 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
EDIT #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
【问题讨论】:
-
你是如何执行控制器的?你正在形成什么网址来访问它?
-
@BhushanBhangale 默认
localhost:8080/BootApp/ -
@bmarkham 您是否将其作为弹簧靴(嵌入式 tomcat)运行?也发布你的 pom.xml 和 application.properties
-
@BalajiKrishnan 编辑了我的帖子
-
请显示您尝试访问的网址和日志。
标签: java spring spring-mvc spring-boot