天天看点

eclipse+maven构建一个简单的springBoot+jsp(只能返回信息)

eclipse+maven构建一个简单的springBoot+jsp(只能返回信息)

程序的结构是这样的

1.建立一个maven项目

这里是如何创建一个maven项目的教程点击打开链接

2.在pom.xml文件中引入需要的java包

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.boot</groupId>
  <artifactId>helloBoot</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>helloBoot Maven Webapp</name>
  <url>http://maven.apache.org</url>
 	<parent>
        <groupId>org.springframework.boot</groupId>
        <!-- 自动包含以下信息: -->
        <!-- 1.使用Java6编译级别 -->
        <!-- 2.使UTF-8编码 -->
        <!-- 3.实现了通用的测试框架 (JUnit, Hamcrest, Mockito). -->
        <!-- 4.智能资源过滤 -->
        <!-- 5.智能的插件配置(exec plugin, surefire, Git commit ID, shade). -->
        <artifactId>spring-boot-starter-parent</artifactId>
        <!-- spring boot 1.x最后稳定版本 -->
        <version>1.5.6.RELEASE</version>
        <!-- 表示父模块pom的相对路径,这里没有值 -->
        <relativePath />
    </parent>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- This is a web application -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Tomcat embedded container-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- Need this to compile JSP -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
 
     <!-- jstl -->
 	<dependency>  
      <groupId>jstl</groupId>  
      <artifactId>jstl</artifactId>  
      <version>1.2</version>  
    </dependency>  
      <dependency>  
      <groupId>taglibs</groupId>  
      <artifactId>standard</artifactId>  
      <version>1.1.2</version>  
    </dependency>
    <dependency>
    	<groupId>javax.servlet</groupId>
    	<artifactId>javax.servlet-api</artifactId>
    	<version>4.0.0</version>
    </dependency>
     <!-- jstl -->
  </dependencies>
  <build>
    <finalName>helloBoot</finalName>
  </build>
</project>
           

3.配置application.properties属性文件

eclipse+maven构建一个简单的springBoot+jsp(只能返回信息)
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

welcome.message: Hello zhoucheng
           

4.编写

SpringBootServletInitializer

执行传统的

WAR

部署运行SpringApplication,

SpringBootWebApplication.java

文件内容如下所示 -

package com.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer {

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

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }

}
           

5. 一个简单的Spring控制器类。

WelcomeController.java

类的代码如下所示 -

package com.boot;

import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@PropertySource(value = "classpath:application.properties")
public class WelcomeController {

    // inject via application.properties
	   @Value("${welcome.message:test}")
    private String message = "Hello World";

    @RequestMapping("/wel")
    public String welcome(Map<String, Object> model,ModelMap m ) {
    	model.put("message", this.message);
    	m.addAttribute("hello","123456");
    	System.out.println(m.get("hello"));
        return "welcome";
    }

}
           
eclipse+maven构建一个简单的springBoot+jsp(只能返回信息)

5.jsp接受后台传来的值

注意:jsp的位置要和你在application.properties属性文件中配置的一样在

/WEB-INF/jsp/
           

目录下否则controller中返回的welcome找不路径

eclipse+maven构建一个简单的springBoot+jsp(只能返回信息)

welcome.jsp中的内容

<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page isELIgnored="false"%> 
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
      <h1>Spring Boot Web JSP Example</h1>
            <h2>${message}</h2>
             <h2>${hello}</h2>
</body>
</html>
           

6.接下来是执行这个程序

右键选中这项目————》Run as————》maven  install

eclipse+maven构建一个简单的springBoot+jsp(只能返回信息)

看到这个就是build success就是构建成功了,然后部署到tomact中

开启tomact

http://localhost:8080/helloBoot/wel

就能看到你想看到的内容了 

继续阅读