搭建Dynamic Web Project(動態web專案)的springmvc工程2
阿新 • • 發佈:2019-02-02
注:開發工具,以及jar包下載,404訪問處理,請到上篇或下連結!!!
下載分享%springmvc開發分享****,(提取碼:2c4a)也可網上下載,適合自己的或最新版本使用。
搭建完的目錄:
注:此處,如果,只有spring的包,而沒有”commons-pool-1.4.jar“和”commons-logging-1.1.1.jar“(版本不限),會在啟動tomcat的時候,出現,找不到日誌類Not found class Log,或者執行緒池錯誤Thread pool。
然後,本篇,具體springmvc備註細節,why?,可上網檢視。
1:使用maven或者,自己匯入專案springmvc支援包,可在分析找到
2:配置web.xml配置檔案:
配置web.xml檔案為:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>tsshare</display-name> <!-- 監聽spring上下文容器 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 載入spring的xml配置檔案到spring的上下文容器中 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/springmvc*.xml</param-value> </context-param> <!-- 配置springmvc DispatcherServlet --> <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/conf/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 配置DispatcherServlet需要攔截的url --> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>*.html</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>
注:此處注意:<url-pattern>*.html</url-pattern>
3:配置springmvc的配置檔案為:4:配置jsp頁面:路徑在”/WEB-INF/views/“下:<?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:tx="http://www.springframework.org/schema/tx" 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-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- springmvc配置 --> <!-- 通過component-scan讓spring掃描package下的所有類,讓spring的註解生效--> <context:component-scan base-package="com.tsxs"></context:component-scan> <!-- 配置springmvc的檢視渲染器,讓其字首為:/ 字尾為: .jsp 將檢視渲染到 /views/<method返回值>.jsp中 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
index.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>Insert title here</title>
</head>
<body>
歡迎頁!
</body>
</html>
home.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>Insert title here</title>
</head>
<body>
我為spring的主頁!
</body>
</html>
home1.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>Insert title here</title>
</head>
<body>
我為spring的主頁~~~1!
</body>
</html>
:5:配置springmvc的控制層類,自動通過annotation掃描,此處為HomeController:
package com.tsxs.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HomeController {
/**
* 首頁 返回至 /views/home.jsp頁面
* */
@RequestMapping("index")
public String toHome(){
System.out.println("let`s go!");
return "home";
}
@RequestMapping("index1")
public ModelAndView toHome1(){
//建立模型和檢視,用於渲染頁面.並指向要返回的頁面為home1
ModelAndView mv = new ModelAndView();
System.out.println("let`s go1!");
mv.setViewName("home1");
return mv;
}
}
注:此處注意:import org.springframework.web.servlet.ModelAndView;
瀏覽器訪問:
http://localhost:8080/tsshare
http://localhost:8080/tsshare/index.html
http://localhost:8080/tsshare/index1.html