重要说明:我正在寻找一个有效的版本,而不是一些代码片段。请撤消重复,因为我对其他主题的答案无能为力(请阅读我的问题的第二部分 - 与资源无关)。
我想创建一个Spring应用程序:
问题:
我的项目结构:
我的代码:
ApplicationLoader.java
package com.stackoverflow.testapp.configuration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class ApplicationLoader extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{ApplicationConfiguration.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
ApplicationConfiguration.java
package com.stackoverflow.testapp.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.stackoverflow.testapp")
public class ApplicationConfiguration {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewresolver = new InternalResourceViewResolver();
viewresolver.setViewClass(JstlView.class);
viewresolver.setPrefix("/WEB-INF/");
viewresolver.setSuffix(".jsp");
return viewresolver;
}
}
DataController.java
package com.stackoverflow.testapp.controllers;
import com.stackoverflow.testapp.models.Data;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class DataController {
@RequestMapping(value = "/api/singledata", method = RequestMethod.GET)
public Data singledata() {
Data singledata = new Data();
return singledata;
}
@RequestMapping(value = "/api/multipledata", method = RequestMethod.GET)
public List<Data> index() {
List<Data> multipledata = new ArrayList<Data>();
multipledata.add(new Data());
multipledata.add(new Data());
multipledata.add(new Data());
return multipledata;
}
}
Data.java
package com.stackoverflow.testapp.models;
public class Data {
public String getData() {
return "Hello world!";
}
}
的index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="UTF-8">
<title>Testapp</title>
<link rel="stylesheet" type="text/css" href="styles/style.css">
</head>
<body>
<p>Hello world!</p>
</body>
</html>
的pom.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow</groupId>
<artifactId>testapp</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>