Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index.jsp</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/data.action?name=餃子">攜帶數據進行頁面跳轉</a>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>main.jsp</title>
</head>
<body>
<h2>顯示頁面跳轉時攜帶的數據......</h2>
<!-- 在經過頁面跳轉后,在跳轉到的頁面里,嘗試獲取之前存放的數據-->
request: ${requestUser}<br>
httpSession: ${sessionUser}<br>
model: ${modelUser}<br>
map: ${mapUser}<br>
modelMap: ${modelMapUser}<br>
<!-- 嘗試直接獲取請求地址中攜帶的參數數據-->
param: ${param.name}
</body>
</html>
package com.example.controller;
import com.example.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
@Controller
public class DataAction {
@RequestMapping("/data")
//這幾個參數都是SpringMVC內置的,可以直接聲明使用
public String data(HttpServletRequest request, HttpSession httpSession, Model model, Map<Object, Object> map, ModelMap modelMap){
//User實體類含有兩個屬性:name(String), age(int)。無參構造方法。全屬性的有參構造方法,getter,setter,toString方法
User user = new User("荷包蛋", 20);
//將user對象利用各SpringMVC內置對象存放到相應作用域中
request.setAttribute("requestUser", user);
httpSession.setAttribute("sessionUser", user);
model.addAttribute("modelUser", user);
map.put("mapUser", user);
modelMap.addAttribute("modelMapUser", user);
//最后完成頁面轉發跳轉
return "main";
}
}
package com.example.controller;
import com.example.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
@Controller
public class DataAction {
@RequestMapping("/data")
public String data(HttpServletRequest request, HttpSession httpSession, Model model, Map<Object, Object> map, ModelMap modelMap){
User user = new User("荷包蛋", 20);
request.setAttribute("requestUser", user);
httpSession.setAttribute("sessionUser", user);
model.addAttribute("modelUser", user);
map.put("mapUser", user);
modelMap.addAttribute("modelMapUser", user);
//最后完成頁面的重定向跳轉
return "redirect:/admin/main.jsp";
}
}
eb開發之MVC設計模式
MVC設計模式是Web應用程序的基本應用架構之一,也是目前在Web開發過程中較為流程的開發架構之一。MVC設計模式與具體所使用的的服務器端語言無關,各主流Web服務器端語言基本上都提供基于MVC設計模式的完整解決方案。通過使用MVC設計模式可以有效實現Web程序前后端的分離,可以提高Web開發的效率與質量。本文主要從MVC基本原理角度對該設計模式進行說明,并給出部分編程語言下MVC的應用實例。
MVC基本概念
MVC設計模式是一種邏輯架構設計模式,它將整個Web應用劃分為三個層次,分別為M、V、C。三字母分別代表Model(模型)、View(視圖)、Controller(控制器)。其中:
M:模型層,主要用于對Web應用程序業務規則進行抽象、封裝與建模,簡單理解Model可以理解為面向對象類,用于實現對業務進行抽象封裝,統一對外提供業務功能。
V:視圖,主要用于輸出展示頁面內容,對于Web應用程序最終輸出的內容為HTML文檔結構,因此視圖主要負責返回用戶瀏覽器HTML頁面信息。
C:控制器,控制器是聯系模型與視圖的橋梁,對于用戶前端交互請求發送到服務器端之后,通過選擇不同的控制器,調用Model中定義的業務邏輯,將運行結果傳遞給View視圖,最終輸出到客戶端。
除以上基本概念與對象之外,在MVC模式下一般還包括路由機制、網頁模板兩類對象概念。它們也是MVC設計模式實現過程的重要組成部分。
1、路由機制
路由從字面理解即為路徑、路途來源等概念。路由在MVC模式下是指針對前端發送的頁面訪問請求,交由合適的控制器去處理。一個企業級應用Web,在MVC模式下每一個控制器對應一個特定的業務邏輯。因此需要準確將訪問請求交由控制器處理。
MVC模式路由機制
2、Web Page模板
前端網頁設計過程中,針對頁面較多情況,往往將各頁面公共部分抽象成網頁的模板,在實際交互操作過程中通過對模板指定位置寫入數據生成視圖,將生成后的結果返回客戶端瀏覽器。
Web page Template
MVC工作過程描述如下圖所示,從用戶發出HTTP訪問請求,到服務器端以HTML、CSS等返回視圖結束,一共需要經歷5個步驟,分別描述為:
MVC模式工作過程描述
1、發送HTTP請求
用戶通過瀏覽器地址欄填寫訪問申請HTTP請求,經過網絡發送到服務器端。絕大部分服務器端支持控制器加方法形式進行訪問,如我們訪問HomeController控制器下的index方法,則可以定義請求http://***.com/Home/index。
2、路由到指定控制器
因為發送的訪問請求是具有特殊格式的,如上一條所給出的示例。在服務器端可從請求中提出出控制器與方法。上例中控制器位HomeController、方法名稱為index,因此可以交由該控制器的index方法進行處理。
3、調用模型(與模型相互作用)
在處理請求的方法,如index中需要針對某一業務進行操作,因此在編寫實現該方法時需要調用封裝好點的業務邏輯規則進行業務處理。即調用model進行業務的處理。
4、控制器調用視圖
控制器調用業務模型處理完成之后,需要將處理結果渲染到View視圖中。在渲染過程中可能需要調用頁面模板,實現對指定內容進行填充。
5、客戶端呈現視圖
視圖渲染完成生成操作結果頁面之后,通過互聯網網絡將HTML等文件直接發送客戶端瀏覽器,由瀏覽器負責解釋。
目前絕大多數服務端編程語言都支持MVC設計模式,如PHP、ASP.NET等。其中PHP第三方服務器框架laravel是使用較為廣泛的MVC設計模式。ASP.NET通過其提供的asp.net mvc 5實現對MVC設計模式的支持。這些基于MVC的設計模式,工作過程與原理基本一致。
本頭條號長期關注編程資訊分享;編程課程、素材、代碼分享及編程培訓。如果您對以上方面有興趣或建議與意見,可以聯系作者,共同探討。期待大家關注!相關文章鏈接如下:
架構設計-PHP+MySQL高負載高并發架構設計的思考
Laravel框架-用戶注冊功能實現(路由、控制器、視圖)
在Spring Boot中,默認的歡迎界面是index.html,那為什么這樣呢?我們可以看看源碼是怎么定義的。
public class WebMvcAutoConfiguration {
private Optional<Resource> getWelcomePage() {
String[] locations = getResourceLocations(this.resourceProperties.getStaticLocations());
return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
}
private Resource getIndexHtml(String location) {
return this.resourceLoader.getResource(location + "index.html");
}
}
從源碼中我們可以看到,歡迎頁的靜態資源文件默認就是index.html頁面,并且只要該頁面存放在resources目錄下的默認路徑中,就會被"/**"映射。
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
/:當前項目的根路徑
也就是只要index.html頁面在以上幾個目錄內,就會自動被Spring Boot探測到。
目錄結構如下,在該項目中,我們在resources目錄下,創建4個子文件夾,具體參考上一章節。然后在每個子文件夾中,都存放一個index.html文件,但是文件內容略有不同,每個文件都有自己的編號。
每個index.html文件內容的編號不同,以此類推!
我們啟動web項目,輸入地址http://localhost:8080會發現,默認加載的是META-INF/resources目錄下的index.html文件,為什么呢?這與靜態資源文件夾的優先級有關系哦,我們上一章節已經講過了
但在實際開發中,我們有時候就希望先訪問登錄界面,然后登錄成功后再跳到主頁面,那此時如何將登錄頁面作為歡迎頁面呢?
這個可以有兩種實現方式。
我們可以在上面的web項目中,創建一個WebMvcConfigurerAdapter類。
package com.yyg.boot.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* @Description Description
* @Author 一一哥Sun
* @Date Created in 2020/3/21
*/
@Configuration
public class DefaultViewConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//這里的"/"是訪問路徑,"forward:home.html"是請求轉發到的頁面名稱
registry.addViewController("/").setViewName("forward:home.html");
//設置優先級
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
super.addViewControllers(registry);
}
}
我們在static目錄下創建一個home.html頁面。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<h1>一一哥的Home頁面...</h1>
</body>
</html>
接著我們運行程序,輸入地址:http://localhost:8080就可以看到如下歡迎界面。
我們在上一個例子的基礎之上,創建一個Controller類。
把上一個案例中DefaultViewConfig配置類的@Configure注解去掉,避免影響到本次實驗。
package com.yyg.boot.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Description Description
* @Author 一一哥Sun
* @Date Created in 2020/3/21
*/
@Controller
public class WelcomeController {
@RequestMapping("/")
public String view() {
return "forward:home.html";
}
}
接著我們運行程序,輸入地址:http://localhost:8080就可以看到如下歡迎界面。
我們可以結合Thymeleaf模板,來實現歡迎頁面。
在該web項目的pom.xml文件中添加依賴包。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
創建application.properties文件,添加如下配置,其實默認也是這個配置。
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
創建login.html存放到/templates/目錄下。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>一一哥的登錄頁面...</h1>
</body>
</html>
package com.yyg.boot.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Description Description
* @Author 一一哥Sun
* @Date Created in 2020/3/21
*/
@Controller
public class WelcomeController {
// @RequestMapping("/")
// public String view() {
// return "forward:home.html";
// }
@RequestMapping("/")
public String login() {
return "login";
}
}
輸入地址,http://localhost:8080即可看到歡迎界面。
很多時候,企業網站一般都會有一個對應的網站圖標(Favicon),在瀏覽器訪問網站時,對應的瀏覽器標簽上會出現對應的圖標。例如csdn網站上的小圖標。
我們可以看看Spring中關于Favicon的源碼。
@Configuration
@ConditionalOnProperty(value = {"spring.mvc.favicon.enabled"},matchIfMissing= true)
public static class FaviconConfiguration implements ResourceLoaderAware {
private final ResourceProperties resourceProperties;
private ResourceLoader resourceLoader;
public FaviconConfiguration(ResourceProperties resourceProperties) {
this.resourceProperties = resourceProperties;
}
public void
setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
@Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(-2147483647);
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));
return mapping;
}
@Bean
public ResourceHttpRequestHandler faviconRequestHandler() {
ResourceHttpRequestHandler
requestHandler = new ResourceHttpRequestHandler();
requestHandler.setLocations(this.resolveFaviconLocations());
return requestHandler;
}
private List<Resource> resolveFaviconLocations() {
String[] staticLocations = WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter.getResourceLocations(this.resourceProperties.getStaticLocations());
List<Resource> locations = new ArrayList(staticLocations.length + 1);
Stream var10000 = Arrays.stream(staticLocations);
ResourceLoader var10001 = this.resourceLoader;
var10001.getClass();
var10000.map(var10001::getResource).forEach(locations::add);
locations.add(new ClassPathResource("/"));
return
Collections.unmodifiableList(locations);
}
}
在SpringBoot 1.x版本中對Favicon進行了默認支持,并且可以通過如下配置進行關閉操作:
spring.mvc.favicon.enabled=false ## 關閉
默認的Favicon圖標效果:
但到了SpringBoot2.x版本后,在Spring Boot項目的issues中提出,如果用戶不進行自定義的Favicon的設置,而Spring Boot項目會提供默認的圖標,而如果提供默認的Favicon圖標,則可能會導致泄露網站的開發框架這樣的信息。
因此,在Spring Boot2.2.x中,將默認的favicon.ico移除,同時也不再提供上述application.properties中的屬性配置。更多詳細信息可查看對應的issues:https://github.com/spring-pr
在2.x以前的版本,直接將你需要的favicon.ico文件存放在static下面就可以。
但到了2.2.X以后的版本,去掉了默認的自動配置,需要我們手動在每一個頁面添加自己網站的Favicon圖標。
我們可以在static目錄下創建一個images目錄,里面存放自己的Favicon.ico圖標。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
<link rel="icon" href="images/Favicon.ico" type="image/x-icon"/>
</head>
<body>
<h1>一一哥的登錄頁面...</h1>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Hello Favicon</title>
<link rel="icon" th:href="@{/favicon.ico}" type="image/x-icon"/>
</head>
<body>
<h1>Hello 一一哥!</h1>
</body>
</html>
我們重新訪問頁面,可以看到Favicon圖標已經換成了我自己的圖標。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。