(一)javascript
有時,記憶的碎片會從腦神經的鏈接中甦醒。html
(二)前端
瀏覽器,客戶端browser,client | view:jsp(含前端控制器DispatcherServlet) | 前端 |
服務器server | 表現層(controller/action),業務層持久層(model:po,service,dao) | 後端 |
設計模式的優勢:java
補充一點,我上面的概括是根據個人理解編寫的,實際狀況的邏輯很複雜。畢竟我認爲java是關係型分層結構的語言。web
(三)spring
準備工具:eclipseapache
建立項目:Maven工程後端
1.properties-java build path設計模式
2.project facets-dynamic web module:2.5(servlet規範,3.0不用web.xml文件) 或 Java EE Tools-Generate Deployment Descriptor Stub-->src-main-webapp- WEB-INF -web.xml.瀏覽器
3.deployment assembly:打包的環境
搭建環境(配置文件):
1.pom.xml
<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>com.buff</groupId> <artifactId>springmvc-work</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <!-- spring版本號 --> <spring.version>4.3.8.RELEASE</spring.version> <!-- jstl標籤版本 --> <jstl.version>1.2</jstl.version> </properties> <dependencies> <!-- springmvc依賴包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <!-- JSTL標籤類 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> <configuration> <!-- tomcat 的端口號 --> <port>8080</port> <!-- 訪問應用的路徑 --> <path>/springmvc-work</path> <!-- URL按UTF-8進行編碼,解決中文參數亂碼 --> <uriEncoding>UTF-8</uriEncoding> <!-- tomcat名稱 --> <server>tomcat7</server> </configuration> </plugin> </plugins> </build> </project>
2.框架核心配置文件:src/main/resources/anyname.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:p="http://www.springframework.org/schema/p" <!-- spring依賴注入的方式:構造方法和set方法。p名稱空間注入本質上屬於set注入,簡化配置 --> xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 配置包掃描controller --> <context:component-scan base-package="com.buff.controller"></context:component-scan> </beans>
3.src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>springmvc-work</display-name> <!-- 在struts2中是過濾器 --> <!-- 配置前端控制器:DispatcherServlet --> <servlet> <servlet-name>springmvcfancyname-work</servlet-name> <!-- Maven依賴包中spring-webmvc --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--加載springmvc主配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:anyname.xml</param-value> </init-param> <!-- 配置tomcat啓動的時候,就加載(建立對象)前端控制器 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <!-- 名稱配對 --> <servlet-name>springmvcfancyname-work</servlet-name> <!-- 配置請求的url規則,說明: 1.*.do,表示以.do結尾的請求,進入前端(的後臺)控制器 2./,表示全部請求都進入前端控制器 --> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- 默認的歡迎頁面 --> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
4.src/main/webapp/WEB-INF/jsp/hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>Demo</title> </head> <body> hello ${hello} <!-- el表達式接收模型數據 --> </body> </html>
5.編寫controller:至關於struts2中處理請求的action
src/main/java/com/buff/controller/DemoController.java
@Controller //對錶現層的註解。最後,對應spring管理文件中的包掃描。 public class DemoController { /** * hello test * ModelAndView:模型和視圖。 * 1.用於設置響應的模型數據; * 2.用於設置響應的視圖 * @RequestMapping:設置請求的url */ @RequestMapping("/hello.do")//響應數據響應頁面有了以後,須要接受方法:建立對應關係 public ModelAndView hello(){ // 1.建立ModelAndView ModelAndView mav = new ModelAndView(); // 2.設置響應的模型數據 /** * addObject方法:設置模型數據 * 參數: * attributeName:模型數據名稱 * attributeValue:模型數據 */ mav.addObject("hello", "world"); // 3.設置響應的視圖 /** * setViewName方法:設置響應的視圖 * 參數: * viewName:頁面的物理路徑 */ mav.setViewName("/WEB-INF/jsp/hello.jsp"); return mav; } }