arranque de primavera vista de la tecnología de capa consolidada JSP, FreeMarker, demostración de introducción thymeleaf

En primer lugar crear un proyecto se puede iniciar normalmente arranque de primavera

https://blog.csdn.net/qq_43560721/article/details/104653470

1.jsp

La estructura total de

pom archivo para agregar dependencias

 <!-- jasper -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

Nuevo paquete de webapp-> WEB-INF> JSP, POJO, controlador, (en las posiciones correspondientes son derecho del ratón)

 

新建 de clase> usuario, UserController

package com.example.demo.pojo;

public class User {
    private Integer id;
    private String name;
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }
    public User(Integer id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + "]";
    }

}
package com.example.demo.controller;

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

import com.example.demo.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class userController {
    @RequestMapping("hello")
    public String hello(ModelMap map) {
        List<User> userList = new ArrayList<>();
        userList.add(new User(1,"小小舍"));
        userList.add(new User(2,"xxs"));
        map.put("userList", userList);
        return "main";
    }
}

nueva JSP

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/3/17 0017
  Time: 17:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<html>
<head>
    <meta charset="utf-8">
    <title>Title</title>
</head>
<body>
<table border="1" align="center" width="50%">
    <tr>
        <td>用户编号</td>
        <td>用户姓名</td>
    </tr>
    <c:forEach items="${userList }" var="u">
        <tr>
            <td>${u.id }</td>
            <td>${u.name }</td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

Encuentra application.properties fichero de configuración para agregar configuración de directorio src / en / principales recursos

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

Seleccione el elemento, haga clic en el botón derecho del ratón y selecciona la pantalla de propiedades del proyecto Abrir configuración Módulo Settiongs, confirme la configuración no hay problema, yo estaba bien en default.

Si no es necesario hacer clic en la esquina superior izquierda de la web "+" para crearlo y luego seleccione la web

Encontrar la clase de arranque, ejecute.

Introduzca en su navegador http: // localhost: 8080 / hola

Accesibles a JSP contenido de la página

pueden producirse errores

Application.properties comprobar su configuración es la capa consistente y estableció sus propios archivos.

 

 

2.freemarker

La estructura total de

pom archivo para agregar dependencias

<!-- freemarker -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-freemarker</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-resources-plugin</artifactId>
			<version>3.1.0</version>
		</dependency>
		
		<!-- jstl -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>

Encuentra application.properties fichero de configuración para agregar configuración de directorio src / en / principales recursos

# 不写也能正常运行,最好加上,有的版本不加就404
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.suffix=.ftl

Sing

package cn.xxs.springbootf.pojo;

public class User {
    private Integer id;
    private String name;
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }
    public User(Integer id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + "]";
    }

}
UserController
package cn.xxs.springbootf.controller;

import cn.xxs.springbootf.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

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


@Controller
public class userController {
    @RequestMapping("/hello")
    public String hello(ModelMap map) {
        List<User> userList = new ArrayList<>();
        userList.add(new User(1,"小小舍"));
        userList.add(new User(2,"xxs"));
        map.put("userList", userList);
        return "freemarker/main";
    }
}

main.ftl

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Insert title here</title>
</head>
<body>
<table border="1" align="center" width="50%" bgcolor="#ffb6c1">
    <tr>
        <td>用户编号</td>
        <td>用户姓名</td>
    </tr>
    <#list userList as u>
        <tr>
            <td>${u.id }</td>
            <td>${u.name }</td>
        </tr>
    </#list>
</table>
</body>
</html>

Comenzar a iniciar la clase

 

pueden producirse errores

El registro de entrada los siguientes lugares

 

3.Thymeleaf

La estructura total de

pom archivo para agregar dependencias

<!-- thymeleaf -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-resources-plugin</artifactId>
			<version>3.1.0</version>
		</dependency>
		<dependency>
    		      <groupId>org.yaml</groupId> 
    		      <artifactId>snakeyaml</artifactId>
		</dependency>
		<!-- jstl -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>
UserController
package cn.xxs.springbootttt.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;



@Controller
public class userController {
    @RequestMapping("user")
    public String user(Model model) {

        model.addAttribute("msg","这是一个Thymeleaf ^-^");

        return "user";
    }
}

user.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<span th:text="${msg}"></span>
</body>
</html>

Clase para empezar a correr

 

 

 

Publicados 141 artículos originales · ganado elogios 33 · Vistas a 50000 +

Supongo que te gusta

Origin blog.csdn.net/qq_43560721/article/details/104894022
Recomendado
Clasificación