Java 使用Maven构建springboot项目

Java 使用Maven构建springboot项目,java,maven,jsp,model-view-controller,spring-boot,Java,Maven,Jsp,Model View Controller,Spring Boot,更新: 谢谢大家的回答。我仍然有问题,因为它没有返回我希望的结果。这是文件放置的问题吗?它们是这样组织的: 提前再次感谢 我学习的目的是使用SpringBoot和JDBC(没有JPA)创建一个web应用程序;我做这件事有几个问题。 我不知道如何正确地创建控制器,以及如何让它返回我想要的.jsp页面。 我找了很多解决方案,但就是找不到合适的。 这是我的pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xml

更新:

谢谢大家的回答。我仍然有问题,因为它没有返回我希望的结果。这是文件放置的问题吗?它们是这样组织的:

提前再次感谢


我学习的目的是使用SpringBoot和JDBC(没有JPA)创建一个web应用程序;我做这件事有几个问题。 我不知道如何正确地创建控制器,以及如何让它返回我想要的.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>---myprojectname</groupId>
    <artifactId>tesieasy</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>tesieasy</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>
我的Servetinizilizer:

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(TesieasyApplication.class);
    }

}
我的实体:

import java.util.Date;
import java.text.SimpleDateFormat;

public class Journal {

    private Long id;
    private String title;
    private Date created;
    private String summary;
    private SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");

    /** costruttori**/
    public Journal() {}

    public Journal(Long id, String title, String summary, Date date){
        this.id = id;
        this.title = title;
        this.summary = summary;
        this.created = date;
    }

    /** getters and setters omitted **/
我的服务:

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import name of my package-.entity.Journal;


@Service
public class JournalService {
    private static final Logger log = LoggerFactory.getLogger(JournalService.class);

    @Autowired
    JdbcTemplate jdbcTemplate;

    public void insertData() {
        log.info("> Table creation");
        jdbcTemplate.execute("DROP TABLE JOURNAL IF EXISTS");
        jdbcTemplate
                .execute("-cut for brevity-");
        log.info("> Done.");
    }

    public List<Journal> findAll() {
        List<Journal> entries = new ArrayList<>();
        jdbcTemplate
                .query("SELECT * FROM JOURNAL",
                        new Object[] {}, (rs, row) -> new Journal(rs.getLong("id"), rs.getString("title"),
                                rs.getString("summary"), new Date(rs.getTimestamp("created").getTime())))
                .forEach(entry -> entries.add(entry));
        return entries;

    }

        public int countEntries() {
        int rowCount = this.jdbcTemplate.queryForObject("SELECT count(*) FROM JOURNAL", Integer.class);
        return rowCount;

    }

    public Journal insertJournal() {
        Journal journal = null;
        this.jdbcTemplate.update(
                -cut for brevity-");
        return journal;

    }
}
import java.util.ArrayList;
导入java.util.Date;
导入java.util.List;
导入org.slf4j.Logger;
导入org.slf4j.LoggerFactory;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.jdbc.core.jdbc模板;
导入org.springframework.stereotype.Service;
导入我的包的名称-.entity.Journal;
@服务
公共类日志服务{
私有静态最终记录器log=LoggerFactory.getLogger(JournalService.class);
@自动连线
jdbc模板jdbc模板;
公共void insertData(){
log.info(“>表创建”);
jdbcTemplate.execute(“如果存在,则删除表日志”);
jdbc模板
.执行(“-cut”表示简洁-”;
log.info(“>完成”);
}
公共列表findAll(){
列表项=新的ArrayList();
jdbc模板
.query(“从日记账中选择*”,
新对象[]{},(rs,row)->新日志(rs.getLong(“id”)、rs.getString(“title”),
rs.getString(“摘要”),新日期(rs.getTimestamp(“已创建”).getTime())
.forEach(条目->条目.add(条目));
返回条目;
}
公共int countEntries(){
int rowCount=this.jdbcTemplate.queryForObject(“从日志中选择计数(*),Integer.class);
返回行计数;
}
公共期刊insertJournal(){
日记账=空;
this.jdbcTemplate.update(
-为简洁起见,删去“;”;
返回日志;
}
}
my.jsp页面之一:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>A title</title></head>
<body>
Hello! JSP is working fine!
</body>
</html>

头衔
您好!JSP工作正常!
最后是我的控制器:

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import *my package name.entity.Journal;
import *my package name.service.JournalService;

@RestController
public class HelloController {

    @Autowired
    private JournalService journalService;

    @RequestMapping(value="/")
    public String home() {
    return "home";
    }

    @RequestMapping("/greeting")
    public String greeting() {
        return "greeting";
    }


    @RequestMapping("/findAll")
    public String index() {
        List<Journal> all;
        all = journalService.findAll();
        return all.toString();
    }

    @RequestMapping("/count")
    public int countElements() {
        int count;
        count = journalService.countEntries();
        return count;
    }

    @RequestMapping("/insert")
    public Journal insertElements() {
        Journal insert;
        insert = journalService.insertJournal();
        return insert;
    }

}
import java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RestController;
导入*我的包名.entity.Journal;
导入*我的包名.service.JournalService;
@RestController
公共类Hello控制器{
@自动连线
私人日记服务;
@请求映射(value=“/”)
公共字符串home(){
返回“家”;
}
@请求映射(“/greeting”)
公共字符串问候语(){
回复“问候”;
}
@请求映射(“/findAll”)
公共字符串索引(){
列出所有;
all=journalService.findAll();
返回所有.toString();
}
@请求映射(“/count”)
公共整数元素(){
整数计数;
count=journalService.countEntries();
返回计数;
}
@请求映射(“/insert”)
公共期刊插页(){
期刊插页;
insert=journalService.insertJournal();
返回插入;
}
}
我的理解是:

  • 我不需要web.xml文件
  • 如何将服务与控制器连接(即使我的findAll方法将我的数据返回到{}中,而且我必须了解如何正确地编写它)
我不明白的是:

  • 为什么它没有正确链接到我的JSP页面-我需要什么
  • 我需要模型和视图吗
我希望你能给我一些建议和/或一些适合我范围的例子。
提前感谢并致以最诚挚的问候!

评论指出,如果要返回要呈现的视图,RestController是不正确的。RestController将以JSON的形式返回对象,而不是以用于AJAX请求的视图id的形式返回。如果要返回视图,只需使用控制器注释即可

您不需要视图,因为它应该将返回值路由到JSP页面。如果要使用JSP标记,则需要一个模型,并且需要向模型对象添加对象。请参见下面的示例。第一个方法应转到welcome.JSP,并且没有可用的模型。第二个方法应转到listall.JSP并创建一个jourNaList属性可用于JSP标记

@Controller
 public class ApplicationController {
    @RequestMapping("/")
    public String index() {
        return "welcome";
    }

    @RequestMapping("/findAll")
    public String index(Model model) {
        List<Journal> all;
        all = journalService.findAll();
        model.addAttribute("journalList", all);
        return "listall";
    }
}
@控制器
公共类应用程序控制器{
@请求映射(“/”)
公共字符串索引(){
返回“欢迎”;
}
@请求映射(“/findAll”)
公共字符串索引(模型){
列出所有;
all=journalService.findAll();
model.addAttribute(“日志列表”,全部);
返回“listall”;
}
}

正如注释所指出的,如果要返回要呈现的视图,RestController是不正确的。RestController将对象返回为JSON,而不是用于AJAX请求的视图id。如果要返回视图,只需使用控制器注释即可

您不需要视图,因为它应该将返回值路由到JSP页面。如果要使用JSP标记,则需要一个模型,并且需要向模型对象添加对象。请参见下面的示例。第一个方法应转到welcome.JSP,并且没有可用的模型。第二个方法应转到listall.JSP并创建一个jourNaList属性可用于JSP标记

@Controller
 public class ApplicationController {
    @RequestMapping("/")
    public String index() {
        return "welcome";
    }

    @RequestMapping("/findAll")
    public String index(Model model) {
        List<Journal> all;
        all = journalService.findAll();
        model.addAttribute("journalList", all);
        return "listall";
    }
}
@控制器
公共类应用程序控制器{
@请求映射(“/”)
公共字符串索引(){
返回“欢迎”;
}
@请求映射(“/findAll”)
公共字符串索引(模型){
列出所有;
all=journalService.findAll();
model.addAttribute(“日志列表”,全部);
返回“lis”
@RequestMapping("/greeting")
public String greeting() {
    return "greeting";
}
@RequestMapping("/findAll")
@ResponseBody
public List<Journal> index() {
    List<Journal> all = journalService.findAll()
    return all;
}
@RequestMapping("/findAll")
@ResponseBody
public Model index(Model model) {
    List<Journal> all =journalService.findAll();
    model.addAttribute("journalList", all);
    return model;
}
@RequestMapping("/findAll")
public String index(Model model) {
    List<Journal> all;
    all = journalService.findAll();
    model.addAttribute("journalList", all);
    return "listall";
}