




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
延遲符SpringMVC簡介SpringMVC入門與核心類什么是SpringMVCSpringMVC框架是圍繞DispatcherServlet
設(shè)計的,DispatcherServlet是SpringMVC的核心控制器,它負責截獲用戶請求,將用戶請求分發(fā)到對應(yīng)的業(yè)務(wù)控制器。利用SpringIoC容器管理SpringMVC框架的業(yè)務(wù)控制器,充分利用IoC容器來降低硬編碼耦合。SpringWebMVC框架提供了大量獨特的功能,包括組件分離、請求處理、視圖渲染和靈活配置等。SpringMVC工作原理SpringMVC是基于JSPModel2實現(xiàn)的技術(shù)框架,利用處理器分離模型、視圖和控制器,達到不同技術(shù)層級間松耦合的效果,提高系統(tǒng)的靈活性、復用性和可維護性。SpringMVC工作原理當用戶在瀏覽器中點擊一個鏈接或者提交一個表單時,那么就會產(chǎn)生一個請求(request)。1請求的第一站到達的是Spring的DispatcherServlet,它是一個前端控制器,工作是將用戶的請求委托給其他的組件去處理。2當選擇了一個合適的控制器后,DispatcherServlet就會將請求交給這個控制器去處理。在這個控制器上,用戶的請求將會將用戶提交的一些信息交由控制器處理并等待。3SpringMVC工作原理當控制器對用戶請求所攜帶的信息進行處理后,經(jīng)常會產(chǎn)生一些其他的需要返回給瀏覽器進行顯示的數(shù)據(jù)。此時需要視圖(View)來對這些數(shù)據(jù)進行顯示了。4DispatcherServlet會向一個視圖解析器(ViewResolver)進行請求,視圖解析器可以將邏輯視圖名稱映射到一個特定的視圖顯示文件上面。5現(xiàn)在DispatcherServlet知道哪一個視圖文件可以顯示結(jié)果了。該視圖將會利用模板數(shù)據(jù)產(chǎn)生輸出。6輸出通過response對象返回給客戶端進行顯示。7謝謝觀看主講人:吳佳云延遲符SpringMVC入門程序SpringMVC入門與核心類SpringMVC入門程序通過一個簡單的入門程序演示SpringMVC的使用。創(chuàng)建項目(1)創(chuàng)建項目單擊IDEA工具欄中的File→New→Project選項,彈出NewProject對話框,填寫項目信息,如圖所示。創(chuàng)建項目(2)創(chuàng)建項目結(jié)構(gòu)單擊IDEA工具欄中的File→New→Directory選項,彈出NewDirectory對話框,添加相應(yīng)的目錄,如圖所示。創(chuàng)建項目(3)創(chuàng)建項目webapp文件夾如果默認創(chuàng)建的Maven項目中沒有自動生成webapp文件夾,可以單擊IDEA工具欄中的File→ProjectStructure選項,彈出ProjectStructure對話框,如圖所示。創(chuàng)建項目(3)創(chuàng)建項目webapp文件夾在Modules的設(shè)置界面中,單擊界面上方的“+”圖標,彈出Add下拉菜單,如圖所示。創(chuàng)建項目(3)創(chuàng)建項目webapp文件夾選中Add下拉菜單,選擇web選項進入web設(shè)置頁面,如圖所示。創(chuàng)建項目(3)創(chuàng)建項目webapp文件夾選中DeploymentDescriptors右側(cè)鉛筆圖樣的編輯按鈕,彈出DeploymentDescriptorsLocation對話框。在DeploymentDescriptorsLocation對話框中,“WebModuleDeploymentDescriptor(web.xml):”輸入框中可以設(shè)置項目web.xml文件的路徑,即修改為“src\main\webapp\WEB-INF\web.xml”,然后單擊“OK”按鈕完成web.xml的路徑的設(shè)置,如圖所示。創(chuàng)建項目(3)創(chuàng)建項目webapp文件夾選中WebResourceDirectories右側(cè)鉛筆圖樣的編輯按鈕,彈出WebResourceDirectoryPath對話框。在WebResourceDirectoryPath對話框中,在WebresoucedirectoryPath:輸入框中設(shè)置項目webapp文件夾路徑,即src\main\webapp,點擊ok按鈕,完成設(shè)置,如圖所示。創(chuàng)建項目(4)項目最終目錄結(jié)構(gòu)引入maven依賴pom.xml<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>jectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>compile</scope>
</dependency>
<!--ApacheCommonsIO-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>配置前端控制器web.xml<!--配置前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置初始化參數(shù),讀取spring-mvc.xml配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!--配置項目啟動時立即加載該servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern><!--配置“/”:攔截所有請求-->
</servlet-mapping>配置SpringMVCspring-mvc.xml<!--配置SpringMVC要掃描的包-->
<context:component-scanbase-package="com.my.ccit.controller"/>
<!--配置視圖解析器-->
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="prefix"value="/WEB-INF/pages/"/>
<propertyname="suffix"value=".jsp"/>
</bean>創(chuàng)建Controller類GreetingController.java@Controller
@RequestMapping("/greeting")
publicclassGreetingController{
@RequestMapping("/hello")
publicStringhello(){
System.out.println("hellonewguys~");
return"hello";
}
}創(chuàng)建視圖success.jsp<body>
<h2>hellonewguys~</h2>
</body>配置Tomcat服務(wù)器點擊EditConfigurations選項,顯示Run/DebugConfigurations對話框。點擊“+”,跳出AddNewConfiguration下拉框,在下拉框中選中Local,如圖所示。配置Tomcat服務(wù)器在TomcatServer頁面添加本地Tomcat,將HTTPport設(shè)為“8888”,如圖所示。選中圖中的Deployment選項卡,選中“+”,添加發(fā)布項目,如圖所示。配置Tomcat服務(wù)器配置完成后,點擊Apply和OK按鈕。點擊頁面的RunTomcat按鈕,啟動項目,如圖所示。項目啟動成功后,瀏覽器中訪問http://localhost:8888/greeting/hello,頁面顯示如圖所示,說明SpringMVC入門程序啟動成功。謝謝觀看主講人:吳佳云延遲符任務(wù)7.1會議申請設(shè)計SpringMVC入門與核心類會議申請設(shè)計
會議申請通常操作是用戶首先提出會議申請,輸入會議信息,系統(tǒng)首先判斷信息填寫是否完整,如果不完整則返回,否則將會議申請信息保存到數(shù)據(jù)庫中。
會議申請模塊主要功能是輸入會議信息,提交會議申請。會議申請模塊按照分層方式設(shè)計,共分為5個步驟:DAO層、Servicice層、視圖層設(shè)計、控制層設(shè)計、Bean的配置。會議申請設(shè)計(1)DAO層設(shè)計@Repository
publicclassMeetingDAOimplementsIMeetingDAO{
@Autowired
privateJdbcTemplatejdbcTemplate;
privateStringnotes;
publicvoidaddMeeting(Meetingmeeting){
finalStringtitle=meeting.getTitle();
finalStringplace=meeting.getPlace();
//省略部分代碼
finalStringnotes=meeting.getNotes();
jdbcTemplate.update(“insertintotb_meeting(title,place,host,particiPant,departMent,meetingLevel,budget,startDate,startTime,endDate,endTime,stauts,approvingPerson,approvingDate,topic,notes)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)”,newPreparedStatementSetter(){
publicvoidsetValues(PreparedStatementps)throwsSQLException{
ps.setString(1,title);
//省略部分代碼
ps.setString(16,notes);
}
});
}
publicJdbcTemplategetJdbcTemplate(){
returnjdbcTemplate;
}
publicvoidsetJdbcTemplate(JdbcTemplatejdbcTemplate){
this.jdbcTemplate=jdbcTemplate;
}
}MeetingDAO.java會議申請設(shè)計(2)Service層設(shè)計@Service
publicclassMeetingServiceimplementsIMeetingService{
@Autowired
privateIMeetingDAOmeetingDAO;
@Autowired
privateMeetingMappermeetingMapper;
publicvoidaddMeeting(Meetingmeeting){
meetingDAO.addMeeting(meeting);
}
publicIMeetingDAOgetMeetingDAO(){
returnmeetingDAO;
}
publicvoidsetMeetingDAO(IMeetingDAOmeetingDAO){
this.meetingDAO=meetingDAO;
}
}MeetingService.java會議申請設(shè)計(3)視圖層設(shè)計會議申請設(shè)計(4)控制層設(shè)計MeetingController.java@Controller
@RequestMapping("/meetingController")
publicclassMeetingController{
privateStringviewPage;
@Autowired
privateMeetingServicemeetingService;
@RequestMapping("/applyMetting")
publicStringapplyMeeting(HttpServletRequestrequest,HttpServletResponseresponse
,Modelmodel)throwsException{
//獲取會議申請信息
Stringtitle=request.getParameter("title");
//……
/*通過請求對象獲取place、host、particiPant、departMent、meetingLevel、budget
、startDate、startTime、endDate、endTime、stauts、approvingPerson
、approvingDate、topic、notes*/
//創(chuàng)建會議對象
Meetingmeeting=newMeeting();
meeting.setTitle(title);
//……
/*meeting對象設(shè)置place、host、particiPant、departMent、meetingLevel、budget
、startDate、startTime、endDate、endTime、stauts、approvingPerson
、approvingDate、topic、notes*/
//調(diào)用會議申請服務(wù)的方法
meetingService.addMeeting(meeting);
model.addAttribute("meeting",meeting);
return"addMeetingSuccess";
}
}會議申請設(shè)計(5)Bean的配置mvc-config.xml<!--配置數(shù)據(jù)源-->
<beanid="dataSource"class="mons.dbcp2.BasicDataSource">
<propertyname="driverClassName"value="${db.driver}"/>
<propertyname="url"value="${db.url}"/>
<propertyname="username"value="${db.username}"/>
<propertyname="password"value="${db.password}"/>
<propertyname="maxIdle"value="5"/>
</bean>
<beanid="jdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate"
p:dataSource-ref="dataSource"/>會議申請設(shè)計
應(yīng)用IDEA開發(fā)工具搭建SpringMVC開發(fā)環(huán)境,設(shè)計并開發(fā)班主任管理中的學生信息管理子模塊,按照SpringMVC設(shè)計分為5個步驟,體會SpringMVC的開發(fā)過程。拓展訓練謝謝觀看主講人:吳佳云延遲符SpringMVC核心類與注解SpringMVC入門與核心類什么是SpringMVC核心類
DispatcherServlet是SpringMVC的核心類,也是SpringMVC的流程控制中心,也被稱為SpringMVC的前端控制器,它可以攔截客戶端的請求。攔截客戶端請求之后,DispatcherServlet會根據(jù)具體規(guī)則將請求交給其他組件處理。
所有請求都要經(jīng)過DispatcherServlet進行轉(zhuǎn)發(fā)處理,這樣就降低了SpringMVC組件之間的耦合性。SpringMVC核心類<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns="/xml/ns/javaee"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/xml/ns/javaee
/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置初始化參數(shù),讀取spring-mvc.xml配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!--配置項目啟動時立即加載該servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern><!--配置“/”:攔截所有請求-->
</servlet-mapping>
</web-app>web.xml什么是SpringMVC注解Spring5對SpringMVC進行了很大增強,現(xiàn)在幾乎完全可以使用基于注解的SpringMVC完全替換掉原來基于接口SpringMVC程序?;谧⒔獾腟pringMVC比之于基于接口的SpringMVC擁有以下幾點好處:方便請求和控制器的映射;方便請求處理方法入?yún)⒔壎║RL參數(shù);Controller不必繼承任何接口,它僅是一個簡單的POJO。SpringMVC常見注解注解說明@Controller標識一個類為控制器(Controller)的注解@RequestMapping可以標注在類定義處,將Controller和特定請求關(guān)聯(lián)起來,還可以標注在方法上,以便進一步對請求進行分流。@RequestParam獲取請求中的參數(shù)值并映射到方法參數(shù)@PathVariable獲取URL中的動態(tài)參數(shù)@RequestBody用于將HTTP請求體中的數(shù)據(jù)綁定到控制器方法的參數(shù)上@ResponesBody用于將方法的返回值直接作為HTTP響應(yīng)的內(nèi)容@RestController是@Controller和@ResponseBody兩個注解的整合。表示控制器類中的所有方法都會返回JSON、XML或其他格式的數(shù)據(jù)@SessionAttributes用于將模型屬性暫時存儲在會話(Session)中的注解@ModelAttribute用于將方法參數(shù)或返回值綁定到web請求中的模型屬性@Controller注解在使用SpringMVC框架開發(fā)Web應(yīng)用程序時,@Controller注解是用來標識一個類為控制器(Controller)的注解??刂破鲗迂撠熃邮沼脩粽埱?,并根據(jù)請求處理邏輯選擇合適的業(yè)務(wù)邏輯進行處理,最終返回響應(yīng)給用戶。@RequestMapping注解真正讓Bean具備SpringMVCController功能的是@RequestMapping這個注解,@RequestMapping可以標注在類定義處,將Controller和特定請求關(guān)聯(lián)起來,還可以標注在方法上,以便進一步對請求進行分流。@RequestMapping注解主要包含以下4個屬性:Value:通過請求的請求地址匹配請求映射。method:通過請求的請求方式來匹配請求映射,請求方式有GET、POST、PUT、DELETE等。Params:通過請求參數(shù)匹配請求。Headers:通過請求頭信息匹配請求。@RequestParam注解@RequestParam注解用于將HTTP請求中的參數(shù)綁定到控制器方法的參數(shù)上,即獲取請求中的參數(shù)值并映射到方法參數(shù)。它支持幾乎所有的數(shù)據(jù)類型,包括基本數(shù)據(jù)類型和復雜數(shù)據(jù)類型。@RequestParam注解主要包含以下4個屬性:
value:name屬性的別名,這里指參數(shù)的名字,如果當前@RequestParam注解只使用vaule屬性,則可以省略value屬性名。
name:綁定的請求參數(shù)名稱。
required:是否必需,默認為true,即請求中必須包含該參數(shù),如果沒有包含,將會拋出異常(可選配置)。
defaultValue:控制器方法形參的默認值,表示如果請求中沒有同名參數(shù)時的默認值。@PathVariable注解@PathVariable注解用于獲取URL中的動態(tài)參數(shù),即將URL中的變量映射到控制器方法的參數(shù)上。這樣就可以通過URL傳遞參數(shù),而不是通過查詢字符串的方式來傳遞參數(shù)。@RequestBody注解@RequestBody注解用于將HTTP請求體中的數(shù)據(jù)綁定到控制器方法的參數(shù)上,即獲取POST請求中的數(shù)據(jù)并映射到方法參數(shù)。它通常用于處理JSON或XML格式的請求體數(shù)據(jù)。@ResponesBody注解@ResponseBody注解用于將方法的返回值直接作為HTTP響應(yīng)的內(nèi)容,而不是通過視圖解析器返回一個視圖@RestController注解@RestController是一個組合注解,它是@Controller和@ResponseBody兩個注解的整合。它表示控制器類中的所有方法都會返回JSON、XML或其他格式的數(shù)據(jù),而不是視圖。@SessionAttributes注解@SessionAttributes注解是用于將模型屬性暫時存儲在會話(Session)中的注解。它可以用在控制器類級別。
names:需要存儲到session中數(shù)據(jù)的名稱。
types:根據(jù)指定參數(shù)的類型,將模型中對應(yīng)類型的參數(shù)存儲到session中。
value:name屬性的別名。@ModelAttribute注解@ModelAttribute注解可以用在方法上和方法的參數(shù)上。被該注解標注的方法會在該controller的請求方法執(zhí)行之前執(zhí)行,執(zhí)行之后的結(jié)果會放在Model中。Model是什么,可以理解為該類的一個全局屬性,是一個map形式。后續(xù)被執(zhí)行的方法可以通過Model獲得該屬性。謝謝觀看主講人:吳佳云延遲符基于SpringMVC注解分析SpringMVC入門與核心類1一個簡單的基于注解的Controller@Controller//<——①
@RequestMapping("/forum")
publicclassBasicController{
@RequestMapping//<——②
publicvoidlistAllBoard(){
System.out.println("calllistAllBoardmethod.");
}
}在①處使用了兩個注解,分別是@Controller和@RequestMapping注解。@Controller注解讓BasicController成為一個Bean。@RequestMapping注解讓BasicController具備SpringMVCController功能。BasicController.java1一個簡單的基于注解的Controller<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置初始化參數(shù),讀取spring-mvc.xml配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!--配置項目啟動時立即加載該servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern><!--配置“/”:攔截所有請求-->
</servlet-mapping>web.xml1一個簡單的基于注解的Controller
<!--配置SpringMVC要掃描的包-->
<context:component-scanbase-package="com.my.ccit.controller"/>
<!--配置視圖解析器-->
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="prefix"value="/WEB-INF/pages/"/>
<propertyname="suffix"value=".jsp"/>
</bean>spring-mvc.xml1一個簡單的基于注解的Controller測試2一個Controller處理多個URL請求@Controller
@RequestMapping("/fornum")
publicclassBasicForumController{
@RequestMapping("/listAllBoard")//<——①
publicvoidlistAllBoard(){
System.out.println("calllistAllBoardmethod.");
}
@RequestMapping("/listBoardTopic")//<——②
publicvoidlistBoardTopic(inttopicId){
System.out.println("calllistBoardTopicmethod.");
}
}上面代碼分別在①和②處為listAllBoard()和listBoardTopic()方法標注了@RequestMapping注解,這樣/fornum/listAllBoard的URL請求將由listAllBoard()負責處理,而/fornum/listBoardTopic?topicId=1的URL請求則由listBoardTopic()方法處理。3一個Controller對應(yīng)一個URL,由請求參數(shù)決定請求處理方法@Controller
@RequestMapping("/fornum")//<——①指定控制器對應(yīng)URL請求
publicclassBasicForumController{
//<—②如果URL請求中包括"method=listAllBoard"的參數(shù),由本方法進行處理
@RequestMapping(params="method=listAllBoard")
publicvoidlistAllBoard(){
System.out.println("calllistAllBoardmethod.");
}
//<——③如果URL請求中包括"method=listBoardTopic"的參數(shù),由本方法進行處理
@RequestMapping(params="method=listBoardTopic")
publicvoidlistBoardTopic(inttopicId){
System.out.println("calllistBoardTopicmethod.");
}
}在①定義處標注的@RequestMapping讓Controller處理所有包含/fornum的URL請求,在②和③處標注的@RequestMapping可以很容易通過URL參數(shù)指定Controller的處理方法了。4讓請求處理方法處理特定的HTTP請求方法這樣只有當/fornum?method=createTopic請求以HTTPPOST方式提交時,createTopic()方法才會進行處理。除了使用@RequestMapping注解的method屬性,來限定當前方法匹配哪種類型的請求方式以外,還可以使用組合注解完成客戶端請求方式的限定。常用的組合注解有:@GetMapping:匹配GET方式的請求。@PostMapping:匹配POST方式的請求。@PutMapping:匹配PUT方式的請求。@DeleteMapping:匹配DELETE方式的請求。@PatchMapping:匹配PATCH方式的請求。@Controller
@RequestMapping("/fornum")
publicclassBasicForumController{
@RequestMapping(params="method=createTopic",method=
RequestMethod.POST)
publicStringcreateTopic(){
System.out.println("callcreateTopicmethod.");
return"createTopic";
}
}4讓請求處理方法處理特定的HTTP請求方法@Controller
@RequestMapping("/fornum")
publicclassBasicForumController{
@PostMapping(params="method=createTopic")
publicStringcreateTopic(){
System.out.println("callcreateTopicmethod.");
return"createTopic";
}
}@PostMapping是@RequestMapping(method=RequestMethod.POST)的縮寫,使用組合注解替代@RequestMapping注解,可以省略method屬性,從而簡化代碼。5按參數(shù)名綁定URL參數(shù)@RequestMapping(params="method=listBoardTopic")
//<——①topicId入?yún)⑹侨绾谓壎║RL請求參數(shù)的?
publicvoidlistBoardTopic(inttopicId){
System.out.println("calllistBoardTopicmethod.");
}當發(fā)送http://localhost/fornum?method=listBoardTopic&topicId=10的URL請求時,Spring不但讓listBoardTopic()方法處理這個請求,而且還將topicId請求參數(shù)在類型轉(zhuǎn)換后綁定到listBoardTopic()方法的topicId形參上。請求處理方法入?yún)⒌念愋涂梢允荍ava基本數(shù)據(jù)類型或String類型,這時方法入?yún)磪?shù)名匹配的原則綁定到URL請求參數(shù),同時還自動完成String類型的URL請求參數(shù)到請求處理方法參數(shù)類型的轉(zhuǎn)換。6請求處理方法的入?yún)⑹且粋€JavaBean這時,如果使用以下URL請求:http://localhost:8888/fornum/list?method=listBoardTopic&topicId=1&userId=10&userName=tom,topicIdURL參數(shù)將綁定到topicId入?yún)⑸?,而userId和userNameURL參數(shù)將綁定到user對象的userId和userName屬性中。和URL請求中不允許沒有topicId參數(shù)不同,雖然User的userId屬性的類型是基本數(shù)據(jù)類型,但如果URL中不存在userId參數(shù),Spring也不會報錯,此時user.userId值為0。如果User對象擁有一個dept.deptId的級聯(lián)屬性,那么它將和dept.deptIdURL參數(shù)綁定。@RequestMapping(value="/list",params="method=listBoardTopic")
publicvoidlistBoardTopic(inttopicId,Useruser){
System.out.println("topicId:"+topicId);
System.out.println("user:"+user);
System.out.println("calllistBoardTopicmethod.");
}@Data
publicclassUser{
privateintuserId;
privateStringuserName;
}7通過注解指定綁定的URL參數(shù)由于對listBoardTopic()請求處理方法的topicId入?yún)俗⒘薂RequestParam("id")注解,所以它將和id的URL參數(shù)綁定。@RequestMapping(value="/list",params="method=listBoardTopic")
publicvoidlistBoardTopic(@RequestParam("id")inttopicId,Useruser){
System.out.println("topicId:"+topicId);
System.out.println("user:"+user);
System.out.println("calllistBoardTopicmethod.");
}8通過注解映射URL綁定的占位符瀏覽器中訪問:http://localhost:8888/fornum/list/1?method=listBoardTopic&userId=10&userName=tom,此時路徑上的1將綁定到topicId入?yún)⑸稀RequestMapping(value="/list/{id}",params="method=listBoardTopic")
publicvoidlistBoardTopic(@PathVariable("id")inttopicId,Useruser){
System.out.println("topicId:"+topicId);
System.out.println("user:"+user);
System.out.println("calllistBoardTopicmethod.");
}通過@PathVariable可以將URL中占位符參數(shù)綁定到控制器處理方法的入?yún)⒅?,即URL中的{xxx}占位符可以通過@PathVariable(“xxx”)綁定到操作方法的入?yún)⒅小?ModelAttribute注解@ModelAttribute是SpringMVC中的一個非常有用的注解,它主要被用來綁定請求參數(shù)到對象上,然后將這個對象添加到模型(Model)中。@ModelAttribute注解的主要作用:綁定請求參數(shù)到對象添加到model中用于數(shù)據(jù)預處理9ModelAttribute注解1綁定請求參數(shù)到對象@RequestMapping(value="/list",params="method=listBoardTopic")
publicvoidlistBoardTopic(@ModelAttribute("user")Useruser){
System.out.println("user:"+user);
System.out.println("calllistBoardTopicmethod.");
}當請求到達控制器方法時,SpringMVC會嘗試將請求參數(shù)綁定到帶有@ModelAttribute注解的方法參數(shù)或?qū)ο蟮膶傩陨?,代碼如下所示。瀏覽器中訪問http://localhost:8888/fornum/list?method=listBoardTopic&userId=10&userName=tom,此時路徑上請求參數(shù)userId=10和userName=tom將被綁定到user入?yún)⑾鄳?yīng)的屬性上。9ModelAttribute注解2添加到model中帶有@ModelAttribute注解的對象會被添加到model中,這樣它就可以在視圖中被訪問。這意味著你可以在視圖中直接使用該對象,而無需在控制器方法中顯式地將它添加到model中,代碼如下所示。@RequestMapping(value="/list/showuser",params="method=listBoardTopic")
publicStringlistBoardTopic(@ModelAttribute("user")Useruser){
System.out.println("user:"+user);
System.out.println("calllistBoardTopicmethod.");
return"userInfo";
}9ModelAttribute注解2添加到model中<form>
<div>
<labelfor="uid">用戶ID:</label>
<inputtype="text"id="uid"name="userId"value="${user.userId}"/>
</div>
<div>
<labelfor="uname">用戶名:</label>
<inputtype="text"id="uname"name="userName"value="${user.userName}"/>
</div>
</form>userInfo.jsp瀏覽器中訪問http://localhost:8888/fornum/list/showuser?method=listBoardTopic&userId=10&userName=tom,頁面如圖所示,說明帶有@ModelAttribute注解的user對象成功被添加到model中。9ModelAttribute注解3用于數(shù)據(jù)預處理可以使用@ModelAttribute注解在控制器方法之前執(zhí)行一些預處理操作,比如從數(shù)據(jù)庫加載數(shù)據(jù)或執(zhí)行其他邏輯。這通常用于填充表單的默認值或進行其他初始化操作。@ModelAttribute("user")
publicUserpopulateUser(){
Useruser=newUser(222,"wangwu");
returnuser;
}
@RequestMapping("/test")
publicvoidtest(Useruser){
System.out.println(user);
}瀏覽器中訪問http://localhost:8888/fornum/test,控制臺輸出,如圖所示,說明@ModelAttribute("user")數(shù)據(jù)預處理成功。10@SessionAttribute注解@SessionAttributes(“user”)注解表示將名為“user”的模型屬性存儲到HTTPsession中。在profile方法中,通過@ModelAttribute將session中的uer對象綁定到入?yún)ser中,從而獲取到存儲在session中的用戶信息。@SessionAttributes注解是SpringMVC提供的一個功能,它允許開發(fā)者將模型屬性存儲在HTTPSession中,使得這些屬性可以在多個請求之間共享。@SessionAttributes("user")//SpringMVC將名為"user"的模型屬性存儲到session中
publicclassMyController{
@GetMapping("/login")
publicvoidlogin(Modelmodel){
Useruser=newUser(333,"zhangsan");
model.addAttribute("user",user);
}
@GetMapping("/profile")
publicvoidprofile(@ModelAttribute("user")Useruser){
//由于"user"屬性被存儲在session中,所以這里可以從session中獲取它
System.out.println(user);
}
@GetMapping("/logout")
publicStringlogout(HttpSessionsession){
//移除Session中的user屬性
session.removeAttribute("user");
return"redirect:/login";
}
}MyController.java謝謝觀看主講人:吳佳云延遲符任務(wù)7.2會議審批設(shè)計SpringMVC入門與核心類會議審批設(shè)計
會議審批通常操作是部門負責人登錄后查看會議列表,點擊“審批”按鈕進入會議審批頁面,根據(jù)會議審批的內(nèi)容判斷是否同意會議,會議審批后進入結(jié)果頁面。
會議審批分為兩個業(yè)務(wù)流程:查詢所有會議審批信息、審批會議審批信息。會議審批模塊按照分層方式設(shè)計,共分為5個步驟:DAO層、Servicice層、視圖層設(shè)計、控制層設(shè)計、Bean的配置。會議審批設(shè)計(1)DAO層設(shè)計MeetingDAO.javapublicbooleanapprovalMeeting(Meetingmeeting){
booleanflag=false;
intid=meeting.getId();
intstauts=0;
StringapprovingPerson="";
StringapprovingDate="";
inti=jdbcTemplate.update(
"updatetb_meetingsetstauts=?,approvingPerson=?,approvingDate=?whereid=?",newObject[]{meeting.getStauts(),
meeting.getApprovingPerson(),
meeting.getApprovingDate(),
meeting.getId()});
if(i>0){
flag=true;
}
returnflag;
}會議審批設(shè)計(2)Service層設(shè)計MeetingService.java//查詢所有會議申請信息
publicListgetAllMeeting(){
returnmeetingDAO.getAllMeeting();
}
//審批會議
publicbooleanapprovalMeeting(Meetingmeeting){
returnmeetingDAO.approvalMeeting(meeting);
}會議審批設(shè)計(3)視圖層設(shè)計會議審批設(shè)計(4)控制層設(shè)計MeetingController.java@RequestMapping("/approvalMeeting")
//審批會議
publicStringapproval(HttpServletRequestrequest,
HttpServletResponseresponse)throwsException{
StringidStr=request.getParameter("id");//獲取id
if(idStr==null){
idStr="0";
}
intid=Integer.parseInt(idStr);
StringstautsStr=request.getParameter("status");
intstauts=Integer.parseInt(stautsStr);
//設(shè)定審批人
StringapprovingPerson="院辦主任";
//審批日期
Datedate=newDate();
SimpleDateFormatsimple=newSimpleDateFormat("yyyy-MM-dd");
StringapprovingDate=simple.format(date);
Meetingmeeting=newMeeting();
meeting.setId(id);
meeting.setStauts(stauts);
meeting.setApprovingPerson(approvingPerson);
meeting.setApprovingDate(approvingDate);
booleanresult=meetingService.approvalMeeting(meeting);
return"approvalSuccess";
}會議審批設(shè)計(5)Bean的配置mvc-config.xml<!--配置數(shù)據(jù)源-->
<beanid="dataSource"class="mons.dbcp2.BasicDataSource">
<propertyname="driverClassName"value="${db.driver}"/>
<propertyname="url"value="${db.url}"/>
<propertyname="username"value="${db.username}"/>
<propertyname="password"value="${db.password}"/>
<propertyname="maxIdle"value="5"/>
</bean>
<beanid="jdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate"
p:dataSource-ref="dataSource"/>會議審批設(shè)計應(yīng)用SpringMVC的MultiActionController類開發(fā)班主任管理模塊中的學生檔案、學生考勤信息子模塊。拓展訓練謝謝觀看主講人:吳佳云延遲符基于XML的SSM框架整合SpringMVC入門與核心類SSM框架整合SSM框架整合主要涉及Spring
MVC框架、Spring框架和MyBatis框架的協(xié)同工作。MyBatis負責與數(shù)據(jù)庫進行交互。Spring負責事務(wù)管理,以及管理持久層的Mapper對象及業(yè)務(wù)層的Service對象。SpringMVC負責管理表現(xiàn)層的Handler。在SSM框架的整合過程中,可以采用兩種主要的方式:基于XML配置和基于注解配置?;赬ML的SSM框架整合(1)搭建數(shù)據(jù)庫環(huán)境CREATETABLE`tb_user3`
(
`id`int(32),
`username`varchar(32),
`address`varchar(32),
PRIMARYKEY(`id`)USINGBTREE
);
INSERTINTO`tb_user3`VALUES(1,'張三','中國江蘇常州');
基于XML的SSM框架整合(2)創(chuàng)建Maven項目<!--spring-webmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>jectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<scope>compile</scope>
</dependency>
<!--ApacheCommonsIO-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency><!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--mybatis-spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.1</version>
</dependency><!--spring-->
<dependency>
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 四川商務(wù)職業(yè)學院《環(huán)境學基礎(chǔ)》2023-2024學年第二學期期末試卷
- 阜陽職業(yè)技術(shù)學院《概率論與數(shù)理統(tǒng)計AW》2023-2024學年第一學期期末試卷
- 河南女子職業(yè)學院《舞蹈鑒賞與批評》2023-2024學年第二學期期末試卷
- 湖南冶金職業(yè)技術(shù)學院《土木水利專業(yè)導論》2023-2024學年第二學期期末試卷
- 浙江工業(yè)職業(yè)技術(shù)學院《建筑裝飾材料與施工工藝》2023-2024學年第一學期期末試卷
- 福建信息職業(yè)技術(shù)學院《模擬商務(wù)談判》2023-2024學年第一學期期末試卷
- 四川省眉山一中辦學共同體2024-2025學年高三下期末考試物理試題(B卷)含解析
- 廣西藍天航空職業(yè)學院《自動化系統(tǒng)概論》2023-2024學年第二學期期末試卷
- 吉林省吉化第一高級中學2025屆高三考前沖刺模擬語文試題試卷含解析
- 福建師范大學《汽車服務(wù)工程專業(yè)導論》2023-2024學年第二學期期末試卷
- 無人機操控技術(shù)(項目式 · 含工作頁) PPT 4-4 DJI地面站操控
- 市政工程計量計價 課件 項目4 管網(wǎng)工程計量與計價
- 基于深度學習的多模態(tài)數(shù)據(jù)融合方法研究
- 醫(yī)療器械倉庫防靜電措施規(guī)范
- GB/T 43493.2-2023半導體器件功率器件用碳化硅同質(zhì)外延片缺陷的無損檢測識別判據(jù)第2部分:缺陷的光學檢測方法
- 2024年DIP管理專項考核試題
- 無創(chuàng)神經(jīng)調(diào)控技術(shù)輔助阿爾茨海默病治療的中國專家共識(2023)要點
- 六宮數(shù)獨題目
- 韓愈簡介完整
- 《學前兒童科學教育》第二章 幼兒科學教育的目標與內(nèi)容課件
- 馬克思主義與社會科學方法論習題與答案
評論
0/150
提交評論