2022-01-21 IDEA创建 Springmvc+JSTL+Tiles 项目

Spring MVC 是 Spring 提供的一个基于 MVC 设计模式的轻量级 Web 开发框架。

JSTL(Java server pages standarded tag library,即JSP标准标签库)是由JCP(Java community Proces)所制定的标准规范,它主要提供给Java Web开发人员一个标准通用的标签库。

Apache Tiles是一个JSP页面布局框架。Tiles框架提供了一种模板机制,可以为某一类页面定义一个通用的模板,该模板定义了页面的整体布局。

Springmvc: https://docs.spring.io/spring-framework/docs/current/reference/html/web.html

JSTL: https://tomcat.apache.org/taglibs/standard/

Tiles: https://tiles.apache.org/


1. 开发环境

    系统:Windows 10 Home

    开发工具:IntelliJ IDEA 2020.1.4 (Community Edition)

    数据库:  MariaDB 10.4.21 (本文内容没有用到数据库)

    Maven 3.8.1 配置:

        Maven安装目录: C:\Applications\java\apache-maven-3.8.1

        本地Maven仓库: C:\Applications\java\maven-repository

    Java version "1.8.0_121"

    Java, Maven 和 IDEA 的安装配置过程,见 IDEA创建Maven Quickstart项目


2. 在 IDEA上创建 Maven Webapp 项目

    New Project -> Project Type: Maven -> Project SDK: 1.8  -> Select maven-archtype-webapp: Next

    Name: SpringmvcTiles

    GroupId: com.example

    ArtifactId: SpringmvcTiles

-> Next

       Maven home Directory: C:\Applications\java\apache-maven-3.8.1

       User settings file: C:\Applications\java\apache-maven-3.8.1\conf\settings.xml

       Local repository: C:\Applications\java\maven-repository

  -> Finish

    生成的项目目录结构,参考 IDEA创建Maven Webapp项目


3. 使用 jetty-maven-plugin, 将 jetty 内嵌运行

    IDEA创建Maven Webapp项目 使用 tomcat7-maven-plugin,本文尝试使用 jetty-maven-plugin。

1) 修改 pom.xml:

   

       

    org.eclipse.jetty

    jetty-maven-plugin

    9.3.7.v20160115

   

        9090

        localhost

   

    1

       

   

*注: path 项目访问路径 本例:localhost:9090, 如果配置的aa,则访问路径为localhost:9090/aa;uriEncoding 非必需项。

2) 运行

Run -> Edit configurations -> Click "+" -> Select "Maven"

    Command line: clean jetty:run

    Name: SpringmvcTiles [clean,jetty:run]

Before Launch:

    Click "+", select "Launch Web Browser"

    Browser: default

    Url: http://localhost:9090

-> OK

    Run -> Run "SpringmvcTiles [clean,jetty:run]"

3) 打包 War 运行

Run -> Edit configurations -> Click "+" -> Select "Maven"

    Command line: clean jetty:run-war

    Name: SpringmvcTiles [clean,jetty:run-war]

Before Launch:

    Click "+", select "Launch Web Browser"

    Browser: default

    Url: http://localhost:9090

-> OK

    Run -> Run "SpringmvcTiles [clean,jetty:run-war]"


4. 导入 spring-webmvc, servlet, jstl, tiles

    访问 http://www.mvnrepository.com/,查询 ...

    修改 pom.xml

    ...

   

    ...

   

   

      javax.servlet

      javax.servlet-api

      4.0.1

      provided

   

    

    

        javax.servlet

        jstl

        1.2

    

    

    

        taglibs

        standard

    1.1.2

    

    

    

        org.apache.tiles

        tiles-jsp

        3.0.7

    


   

   

      org.springframework

      spring-web

      4.3.9.RELEASE

   

   

   

      org.springframework

      spring-webmvc

      4.3.9.RELEASE

   

   

   

      org.springframework

      spring-jdbc

      4.3.9.RELEASE

   

    

    

        org.springframework

        spring-orm

        4.3.9.RELEASE

    

    ...

   

    ...

在IDE中项目列表 -> 点击鼠标右键 -> Maven -> Reload Project


5. 支持 SpringMVC

1) 修改 src/main/webapp/WEB-INF/web.xml

    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 

    "http://java.sun.com/dtd/web-app_2_3.dtd" > 

 

  ...

   

     

        springMVC 

        org.springframework.web.servlet.DispatcherServlet 

         

            contextConfigLocation 

            classpath:springmvc-beans.xml 

         

        1 

     

     

        springMVC 

        / 

   

    ...

2) 添加 src/main/resources/springmvc-beans.xml  (中间目录如果不存在,新建目录,下同)

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      xmlns:mvc="http://www.springframework.org/schema/mvc"

      xmlns:context="http://www.springframework.org/schema/context"

      xsi:schemaLocation="

        http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context.xsd

        http://www.springframework.org/schema/mvc

        http://www.springframework.org/schema/mvc/spring-mvc.xsd"

      default-init-method="init"

      default-destroy-method="destroy">

   

   

   

    

    

        

        

    


6. 支持静态资源 (html/js/css/images)

    1) 修改 src/main/resources/springmvc-beans.xml

        

            ...

            

            

            

            ...

        

    新建目录  src/main/webapp/static

2) 添加 src/main/webapp/static/test.html

   

    Static HTML

    

Static HTML Page - Test

    

 

    http://localhost:9090/static/test.html

3) 添加 jQuery

        从  https://jquery.com/ 下载 JQuery 包,添加到:

            src/main/webapp/static/js/jquery-1.12.2.min.js

        * jQuery版本根据项目需要,这里用1.12.2, CSS和图片也是放到static目录下


7. 支持 Tiles

    1) 修改 src/main/resources/springmvc-beans.xml

    

        ...

        

        

            

                

                    classpath:tiles-definitions.xml

                

            

        

        ...

    

3) 添加 src/main/resources/tiles-definitions.xml

"-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"

"http://tiles.apache.org/dtds/tiles-config_3_0.dtd">

    

    

    

    

4) 页面布局

    (1) 添加 src/main/webapp/WEB-INF/jsp/layout/header.jsp

        <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"  isELIgnored="false" %>

        

        

        

            

            Springmvc Tiles

            

        

            

                

Header

    (2) 添加 src/main/webapp/WEB-INF/jsp/layout/top_navbar.jsp

        

Top Navbar

    (3) 添加 src/main/webapp/WEB-INF/jsp/layout/top_menu.jsp

        

Top Menu

    (4) 添加 src/main/webapp/WEB-INF/jsp/layout/footer.jsp

            

Footer

        

        


8. View & Controller

    1) 添加 src/main/webapp/WEB-INF/jsp/home.jsp

        <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"  isELIgnored="false" %>

        <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

        <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>

        

        

        

            

Home Page

            

URL: ${request.getContextPath()}

            

                

${message}

            

            

 

        

        

    2) 添加 src/main/java/com/example/controller/IndexController.java

        package com.example.controller;

        import org.springframework.stereotype.Controller;

        import org.springframework.web.bind.annotation.RequestMapping;

        import org.springframework.web.bind.annotation.RequestMethod;

        mport org.springframework.ui.ModelMap;

        @Controller

        @RequestMapping("/")

        public class IndexController {

            @RequestMapping(method = RequestMethod.GET)

            public String defaultIndex(ModelMap modelMap) {

                modelMap.addAttribute("message", "Springmvc Tiles Demo");

                return "home";

            }

        }


9. 运行

    跳转到第 3 步,运行。

    Tomcat 环境下,注解里的 “/” 和 src/main/webapp/index.jsp 会只认 index.jsp,可以删除 index.jsp, 或在 WEB-INF/web.xml 添加:

      

        springMVC

      

你可能感兴趣的:(2022-01-21 IDEA创建 Springmvc+JSTL+Tiles 项目)