2022-01-21 IDEA創(chuàng)建 Springmvc+JSTL+Tiles 項(xiàng)目

Spring MVC 是 Spring 提供的一個(gè)基于 MVC 設(shè)計(jì)模式的輕量級(jí) Web 開發(fā)框架。

JSTL(Java server pages standarded tag library,即JSP標(biāo)準(zhǔn)標(biāo)簽庫(kù))是由JCP(Java community Proces)所制定的標(biāo)準(zhǔn)規(guī)范,它主要提供給Java Web開發(fā)人員一個(gè)標(biāo)準(zhǔn)通用的標(biāo)簽庫(kù)。

Apache Tiles是一個(gè)JSP頁(yè)面布局框架。Tiles框架提供了一種模板機(jī)制,可以為某一類頁(yè)面定義一個(gè)通用的模板,該模板定義了頁(yè)面的整體布局。

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.?開發(fā)環(huán)境

? ? 系統(tǒng):Windows 10 Home

? ? 開發(fā)工具:IntelliJ IDEA 2020.1.4 (Community Edition)

? ? 數(shù)據(jù)庫(kù):? MariaDB 10.4.21 (本文內(nèi)容沒有用到數(shù)據(jù)庫(kù))

? ? Maven 3.8.1 配置:

????????Maven安裝目錄: C:\Applications\java\apache-maven-3.8.1

????????本地Maven倉(cāng)庫(kù): C:\Applications\java\maven-repository

? ? Java version "1.8.0_121"

? ? Java, Maven 和 IDEA 的安裝配置過程,見?IDEA創(chuàng)建Maven Quickstart項(xiàng)目


2. 在 IDEA上創(chuàng)建 Maven Webapp 項(xiàng)目

????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

????生成的項(xiàng)目目錄結(jié)構(gòu),參考?IDEA創(chuàng)建Maven Webapp項(xiàng)目


3. 使用 jetty-maven-plugin, 將 jetty 內(nèi)嵌運(yùn)行

? ??IDEA創(chuàng)建Maven Webapp項(xiàng)目?使用?tomcat7-maven-plugin,本文嘗試使用 jetty-maven-plugin。

1) 修改 pom.xml:

<project ... >

<build>

? ? <plugins>

? ? ? ? <plugin>

? ? <groupId>org.eclipse.jetty</groupId>

? ? <artifactId>jetty-maven-plugin</artifactId>

? ? <version>9.3.7.v20160115</version>

<configuration>

? ? <httpConnector>

? ? ? ? <port>9090</port>

? ? ? ? <host>localhost</host>

? ? </httpConnector>

? ? <scanIntervalSeconds>1</scanIntervalSeconds>

</configuration>

? ? ? ? </plugin>

? ? </plugins>

</build>

</project>

*注: path 項(xiàng)目訪問路徑 本例:localhost:9090, 如果配置的aa,則訪問路徑為localhost:9090/aa;uriEncoding 非必需項(xiàng)。

2) 運(yùn)行

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 運(yùn)行

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. 導(dǎo)入 spring-webmvc, servlet, jstl, tiles

????訪問 http://www.mvnrepository.com/,查詢 ...

????修改 pom.xml

<project ... >

????...

? ? <dependencies>

? ? ...

? ? <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->

? ? <dependency>

? ? ? <groupId>javax.servlet</groupId>

? ? ? <artifactId>javax.servlet-api</artifactId>

? ? ? <version>4.0.1</version>

? ? ? <scope>provided</scope>

? ? </dependency>

????<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->

????<dependency>

????????<groupId>javax.servlet</groupId>

????????<artifactId>jstl</artifactId>

????????<version>1.2</version>

????</dependency>

????<!-- https://mvnrepository.com/artifact/taglibs/standard -->

????<dependency>

????????<groupId>taglibs</groupId>

????????<artifactId>standard</artifactId>

????<version>1.1.2</version>

????</dependency>

????<!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-jsp -->

????<dependency>

????????<groupId>org.apache.tiles</groupId>

????????<artifactId>tiles-jsp</artifactId>

????????<version>3.0.7</version>

????</dependency>


? ? <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->

? ? <dependency>

? ? ? <groupId>org.springframework</groupId>

? ? ? <artifactId>spring-web</artifactId>

? ? ? <version>4.3.9.RELEASE</version>

? ? </dependency>

? ? <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->

? ? <dependency>

? ? ? <groupId>org.springframework</groupId>

? ? ? <artifactId>spring-webmvc</artifactId>

? ? ? <version>4.3.9.RELEASE</version>

? ? </dependency>

? ? <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->

? ? <dependency>

? ? ? <groupId>org.springframework</groupId>

? ? ? <artifactId>spring-jdbc</artifactId>

? ? ? <version>4.3.9.RELEASE</version>

? ? </dependency>

????<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->

????<dependency>

? ? ????<groupId>org.springframework</groupId>

? ? ????<artifactId>spring-orm</artifactId>

? ? ????<version>4.3.9.RELEASE</version>

????</dependency>

? ? ...

? ? </dependencies>

????...

</project>

在IDE中項(xiàng)目列表 -> 點(diǎn)擊鼠標(biāo)右鍵 -> Maven -> Reload Project


5. 支持 SpringMVC

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

<!DOCTYPE web-app PUBLIC?

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

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

<web-app>?

? ...

? <!-- Spring mvc 適配器 -->?

? ? <servlet>?

? ? ? ? <servlet-name>springMVC</servlet-name>?

? ? ? ? <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>?

? ? ? ? <init-param>?

? ? ? ? ? ? <param-name>contextConfigLocation</param-name>?

? ? ? ? ? ? <param-value>classpath:springmvc-beans.xml</param-value>?

? ? ? ? </init-param>?

? ? ? ? <load-on-startup>1</load-on-startup>?

? ? </servlet>?

? ? <servlet-mapping>?

? ? ? ? <servlet-name>springMVC</servlet-name>?

? ? ? ? <url-pattern>/</url-pattern>?

? ? </servlet-mapping>

? ? ...

</web-app>

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

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

? ? ? 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">

? ? <!-- Scan package -->

? ? <context:component-scan base-package="com.example"/>

? ? <mvc:annotation-driven />

????<!-- MVC viewResolver -->

????<bean id="viewResolver" ????????class="org.springframework.web.servlet.view.InternalResourceViewResolver">

????????<property name="prefix" value="/WEB-INF/jsp/"/>

????????<property name="suffix" value=".jsp"/>

????</bean>

</beans>


6. 支持靜態(tài)資源?(html/js/css/images)

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

????????<beans ...>

????????????...

? ? ????????<!-- html,css,js,images -->

? ? ????????<mvc:resources location="/static/" mapping="/static/**" />

? ? ????????<mvc:default-servlet-handler />

????????????...

????????</beans>

? ? 新建目錄? src/main/webapp/static

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

<!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=UTF-8">

? ? <title>Static HTML</title>

</head>

<body>

????<h3>Static HTML Page - Test</h3>

????<p>&nbsp;</p>

</body>

</html>

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

3) 添加 jQuery

? ? ? ? 從??https://jquery.com/?下載 JQuery 包,添加到:

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

????????* jQuery版本根據(jù)項(xiàng)目需要,這里用1.12.2, CSS和圖片也是放到static目錄下


7. 支持 Tiles

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

????<beans ...>

????????...

????????<!-- tiles tag? -->

????????<bean id="tilesConfigurer" ????????????class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">

????????????<property name="definitions">

????????????????<list>

????????????????????<value>classpath:tiles-definitions.xml</value>

????????????????</list>

????????????</property>

????????</bean>

????????...

????</beans>

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

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE tiles-definitions PUBLIC

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

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

<tiles-definitions>

????<definition name="header" template="/WEB-INF/jsp/layout/header.jsp"></definition>

????<definition name="top_navbar" template="/WEB-INF/jsp/layout/top_navbar.jsp"></definition>

????<definition name="top_menu" template="/WEB-INF/jsp/layout/top_menu.jsp"></definition>

????<definition name="footer" template="/WEB-INF/jsp/layout/footer.jsp"></definition>

</tiles-definitions>

4) 頁(yè)面布局

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

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

????????<!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=UTF-8">

????????????<title>Springmvc Tiles</title>

????????????<script language="javascript" src="${request.getContextPath()}/static/js/jquery-1.12.2.min.js"></script>

????????</head>

????????????<body id="app-layout">

? ? ? ? ? ? ????<p style="background-color: gray; padding: 5px; color: white;">Header</p>

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

????????<p style="background-color: blue; padding: 5px; color: white;">Top Navbar</p>

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

????????<p style="background-color: red; padding: 5px; color: white;">Top Menu</p>

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

????????????<p style="background-color: gray; padding: 5px; color: white;">Footer</p>

????????</body>

????????</html>


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" %>

????????<tiles:insertDefinition name="header" />

????????<tiles:insertDefinition name="top_navbar" />

????????<tiles:insertDefinition name="top_menu" />

????????????<h3>Home Page</h3>

????????????<p>URL: ${request.getContextPath()} </p>

????????????<c:if test="${not empty message}">

????????????????<p>${message}</p>

????????????</c:if>

????????????<p>&nbsp;</p>

????????<script type="text/javascript">

????????????$(document).ready(function() {

????????????????console.log("Home Page");

????????????});

????????</script>

????????<tiles:insertDefinition name="footer" />

????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. 運(yùn)行

? ? 跳轉(zhuǎn)到第 3 步,運(yùn)行。

? ? Tomcat 環(huán)境下,注解里的 “/” 和 src/main/webapp/index.jsp 會(huì)只認(rèn) index.jsp,可以刪除 index.jsp, 或在 WEB-INF/web.xml 添加:

? ????<welcome-file-list>

? ? ????<welcome-file>springMVC</welcome-file>

? ????</welcome-file-list>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容