原创不易,转载请注明出处:SpringMVC入门教程及其原理讲解和实例代码下载
代码下载地址:http://www.zuidaima.com/share/1751859714182144.htm
SpringMVC框架介绍
Spring框架提供了构造Web应用程序的全能MVC模块。Spring MVC分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行制定。是一个标准的MVC框架。
SpringMVC框架图
那你猜一猜哪一部分应该是哪一部分?
SpringMVC接口解释
DispatcherServlet接口:
Spring提供的前端控制器,所有的请求都有经过它来统一分发。在DispatcherServlet将请求分发给Spring Controller之前,需要借助于Spring提供的HandlerMapping定位到具体的Controller。
HandlerMapping接口:
能够完成客户请求到Controller映射。
Controller接口:
需要为并发用户处理上述请求,因此实现Controller接口时,必须保证线程安全并且可重用。Controller将处理用户请求,这和Struts Action扮演的角色是一致的。一旦Controller处理完用户请求,则返回ModelAndView对象给DispatcherServlet前端控制器,ModelAndView中包含了模型(Model)和视图(View)。从宏观角度考虑,DispatcherServlet是整个Web应用的控制器;从微观考虑,Controller是单个Http请求处理过程中的控制器,而ModelAndView是Http请求过程中返回的模型(Model)和视图(View)。
ViewResolver接口:
Spring提供的视图解析器(ViewResolver)在Web应用中查找View对象,从而将相应结果渲染给客户。
SpringMVC运行原理
1. 客户端请求提交到DispatcherServlet
2. 由DispatcherServlet控制器查询一个或多个HandlerMapping,找到处理请求的Controller
3. DispatcherServlet将请求提交到Controller
4. Controller调用业务逻辑处理后,返回ModelAndView
5. DispatcherServlet查询一个或多个ViewResoler视图解析器,找到ModelAndView指定的视图
6. 视图负责将结果显示到客户端
SpringMVC运行实例
Account类:
01 |
package com.pb.entity;
|
02 |
03 |
public class Account {
|
04 |
private String cardNo;
|
05 |
private String password;
|
06 |
private float balance;
|
07 |
public String getCardNo() {
|
08 |
return cardNo;
|
09 |
}
|
10 |
public void setCardNo(String cardNo) {
|
11 |
this .cardNo = cardNo;
|
12 |
}
|
13 |
public String getPassword() {
|
14 |
return password;
|
15 |
}
|
16 |
public void setPassword(String password) {
|
17 |
this .password = password;
|
18 |
}
|
19 |
public float getBalance() {
|
20 |
return balance;
|
21 |
}
|
22 |
public void setBalance( float balance) {
|
23 |
this .balance = balance;
|
24 |
}
|
25 |
|
26 |
} |
LoginController类:
01 |
package com.pb.web.controller;
|
02 |
03 |
import java.util.HashMap;
|
04 |
import java.util.Map;
|
05 |
06 |
import javax.servlet.http.HttpServletRequest;
|
07 |
import javax.servlet.http.HttpServletResponse;
|
08 |
09 |
import org.springframework.web.servlet.ModelAndView;
|
10 |
import org.springframework.web.servlet.mvc.AbstractController;
|
11 |
12 |
import com.pb.entity.Account;
|
13 |
14 |
public class LoginController extends AbstractController {
|
15 |
private String successView;
|
16 |
private String failView; //这两个参数是返回值传给applicationContext.xml,进行页面分配
|
17 |
|
18 |
public String getSuccessView() {
|
19 |
return successView;
|
20 |
}
|
21 |
public void setSuccessView(String successView) {
|
22 |
this .successView = successView;
|
23 |
}
|
24 |
public String getFailView() {
|
25 |
return failView;
|
26 |
}
|
27 |
public void setFailView(String failView) {
|
28 |
this .failView = failView;
|
29 |
}
|
30 |
@Override
|
31 |
protected ModelAndView handleRequestInternal(HttpServletRequest request,
|
32 |
HttpServletResponse response) throws Exception {
|
33 |
// TODO Auto-generated method stub
|
34 |
String cardNo=request.getParameter( "cardNo" );
|
35 |
String password=request.getParameter( "password" );
|
36 |
Account account =getAccount(cardNo,password);
|
37 |
Map<String ,Object> model= new HashMap<String,Object>();
|
38 |
if (account != null ){
|
39 |
model.put( "account" , account);
|
40 |
return new ModelAndView(getSuccessView(),model);
|
41 |
} else {
|
42 |
model.put( "error" , "卡号和密码不正确" );
|
43 |
return new ModelAndView(getFailView(),model);
|
44 |
}
|
45 |
} //本应该这个方法写在模型层,这地方直接给放在了逻辑层这个地方偷懒了。
|
46 |
public Account getAccount(String cardNo,String password){
|
47 |
if (cardNo.equals( "123" )&&password.equals( "123" )){
|
48 |
Account account = new Account();
|
49 |
account.setCardNo(cardNo);
|
50 |
account.setBalance( 88 .8f);
|
51 |
return account;
|
52 |
} else {
|
53 |
return null ;
|
54 |
}
|
55 |
}
|
56 |
57 |
} |
applicationContext.xml
01 |
<?xml version= "1.0" encoding= "UTF-8" ?>
|
02 |
03 |
<beans xmlns= "http://www.springframework.org/schema/beans"
|
04 |
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
|
05 |
xmlns:aop= "http://www.springframework.org/schema/aop"
|
06 |
xmlns:tx= "http://www.springframework.org/schema/tx"
|
07 |
xsi:schemaLocation="http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
|
08 |
http: //www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
|
09 |
http: //www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
|
10 |
<bean id= "loginController" class = "com.pb.web.controller.LoginController" >
|
11 |
<property name= "successView" value= "showAccount" ></property>
|
12 |
<property name= "failView" value= "login" ></property>
|
13 |
</bean>
|
14 |
<bean id= "urlMapping" class = "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
|
15 |
<property name= "mappings" >
|
16 |
<props>
|
17 |
<prop key= "/login.do" >loginController</prop>
|
18 |
</props>
|
19 |
</property>
|
20 |
</bean>
|
21 |
<bean id= "viewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >
|
22 |
<property name= "prefix" value= "/" ></property>
|
23 |
<property name= "suffix" value= ".jsp" ></property>
|
24 |
</bean>
|
25 |
</beans> |
Jsp页面:
01 |
<%@ page language= "java" contentType= "text/html; charset=GB18030"
|
02 |
pageEncoding= "GB18030" %>
|
03 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
|
04 |
<html> |
05 |
<head> |
06 |
<meta http-equiv= "Content-Type" content= "text/html; charset=GB18030" >
|
07 |
<title>Insert title here</title> |
08 |
</head> |
09 |
<body> |
10 |
<a href= "login.jsp" >进入</a>
|
11 |
12 |
</body> |
13 |
</html> |
login.jsp
01 |
<%@ page language= "java" contentType= "text/html; charset=GB18030"
|
02 |
pageEncoding= "GB18030" %>
|
03 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
|
04 |
<html> |
05 |
<head> |
06 |
<meta http-equiv= "Content-Type" content= "text/html; charset=GB18030" >
|
07 |
<title>Insert title here</title> |
08 |
</head> |
09 |
<body> |
10 |
${error } |
11 |
<form action= "login.do" method= "post" >
|
12 |
账号登陆<br>
|
13 |
<hr>
|
14 |
卡号:<input type= "text" name= "cardNo" ><br>
|
15 |
密码:<input type= "text" name= "password" ><br>
|
16 |
<input type= "submit" value= "登陆" >
|
17 |
</form>
|
18 |
19 |
</body> |
20 |
</html> |
showAccount.jsp
01 |
<%@ page language= "java" contentType= "text/html; charset=GB18030"
|
02 |
pageEncoding= "GB18030" %>
|
03 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
|
04 |
<html> |
05 |
<head> |
06 |
<meta http-equiv= "Content-Type" content= "text/html; charset=GB18030" >
|
07 |
<title>Insert title here</title> |
08 |
</head> |
09 |
<body> |
10 |
账户信息<br>
|
11 |
卡号:${account.cardNo }<br>
|
12 |
密码:${account.password }<br>
|
13 |
钱数:${account.balance }<br>
|
14 |
</body> |
15 |
</html> |
Web.xml
01 |
<? xml version = "1.0" encoding = "UTF-8" ?>
|
02 |
< web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
|
03 |
xmlns = "http://java.sun.com/xml/ns/j2ee"
|
04 |
xmlns:javaee = "http://java.sun.com/xml/ns/javaee"
|
05 |
xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version = "2.4" >
|
06 |
< welcome-file-list >
|
07 |
< welcome-file >index.jsp</ welcome-file >
|
08 |
</ welcome-file-list >
|
09 |
< servlet >
|
10 |
< servlet-name >Dispatcher</ servlet-name >
|
11 |
< servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class >
|
12 |
< init-param >
|
13 |
< param-name >contextConfigLocation</ param-name >
|
14 |
< param-value >classpath:applicationContext.xml</ param-value >
|
15 |
</ init-param >
|
16 |
</ servlet >
|
17 |
< servlet-mapping >
|
18 |
< servlet-name >Dispatcher</ servlet-name >
|
19 |
< url-pattern >*.do</ url-pattern >
|
20 |
</ servlet-mapping >
|
21 |
</ web-app >
|
工程原码:原码
SpringMVC总结
以上就是我理解的Spring MVC可能不够深刻。其实对于任何的框架来说,一个框架是一个可复用设计,框架的最大的好处就是复用。每个框架都有存在的理由,那Spring MVC的理由是什么呢?
只有各个框架之间彼此了解他们之间的优缺点,使用场合,使用原理,才能让我们的更快的成长。
相关推荐
java练习题
云南省移动应用大赛模板.zip
前台技术框架采用Bootstrap,一个高度灵活的HTML5响应式框架,为用户提供了流畅的前端交互体验。程序开发环境支持多样化,无论是myEclipse、Eclipse还是Idea都能轻松应对,结合mysql数据库,确保了数据的高效处理与存储。后台架构则选用SSM组合——SpringMVC、Spring和Mybatis,这一组合以其稳定性和高效性而备受青睐。 校园公益信息关联系统采用b/s架构,实现用户信息、活动类型、公益活动、活动报名、捐款、捐款统计、留言和新闻信息的全面管理。系统分为前台学生端和后台管理员端,满足不同用户群体的需求。 管理员端功能丰富,包括学院管理、活动类型管理、公益活动管理、活动报名管理、捐款信息管理、管理员账号管理、密码修改、捐款统计管理、留言管理和新闻信息管理等。管理员能够灵活添加、修改、删除和查询各类信息,确保信息的准确性和时效性。同时,捐款统计功能以直观的统计图形式展现,为管理员提供决策支持。 学生端则专注于学生的日常需求,包括添加捐款信息、留言、报名活动以及密码修改等。学生可以轻松完成捐款操作,发表留言,查看并报名公益活动,随时修改个人密码,确保账
JavaWeb程序设计SSM框架选课系统开发大作业有数据库文
行业分析报告
1、嵌入式物联网ESP32项目实战开发。例程经过精心编写,简单好用。 2、代码使用Visual Studio Code + ESP-IDF开发,C语言编程。例程在ESP32-S3上运行。若在其他型号上运行,请自行调整。 3、如果接入其他传感器,请查看发布的其他资料。 4、ESP32与模块的接线,在代码当中均有定义,请自行对照。 5、若硬件差异,请根据自身情况适当调整代码,程序仅供参考。 6、代码有注释说明,请耐心阅读。 7、技术v:349014857;
USB无线网卡驱动 USB\VID_1A86&PID_E397&REV_0738
TA-Lib(Technical Analysis Library, 即技术分析库)是Python金融量化的高级库,涵盖了150多种股票、期货交易软件中常用的技术分析指标,如MACD、RSI、KDJ、动量指标、布林带等。但很多人安装指标计算ta-lib库就总报错,就可以在这里找到包下载后安装。 文件举例:TA_Lib‑0.4.24‑cp37‑cp37m‑win_amd64.whl 命名解释:包名-版本号-cp37代表适用于python3.7版本-win代表windows平台-amd64表示64位版本(与python版本要一致) 假定文件下载到d盘根目录,使用如下命令进行安装: pip install d:\TA_Lib‑0.4.24‑cp37‑cp37m‑win_amd64.whl
电子通信设计资料电动智能小车设计论文资料提取方式是百度网盘分享地址
调节篮球比赛定时器,毕业设计实验报告,multisim仿真,AD09原理图及PCB图
编程题实训-串
汉诺塔c语言递归
行业分析报告
电子通信设计资料单片机串行通信发射机论文资料提取方式是百度网盘分享地址
完整代码!扫雷游戏,vs2010使用vs2010开发小游戏,这是一个扫雷的游戏,适应于大作业和毕业论文.zip
基于JAVA毕业设计-JAVA图书馆书库管理系统设计(论文+源代码).rar 毕业设计(论文)是考核应考者综合运用所学基础理论和专业技能,独立分析和解决实际问题的能力。计算机应用专业培养从事计算机软件和硬件设计,开发和应用的高层次人才,检测考生是否阅读了必要的中外文献,能否运用科技合理的定性和定量分析,来设计和实现设计系统。 图书馆书库管理系统主要是完成图书管理员对图书的管理(增加新书,删除旧书,并修改等的图书编辑);图书管理员对读者借还书的统计(图书的在库数目和还日期的统计)和管理;读者和管理员对图书信息和读者信息的查询;当查到所需信息时,打印出相应的信息报表等工作。 在图书馆书库管理系统的设计与实现过程中,我深深体会到此次毕业设计的重要性------它是我走上工作岗位前的一次重要的练习,更深刻体会到理论联系实践的重要性和必要性。同时,我也感受到JAVA 和SQL SERVER 2000 的功能之强大,事件处理的灵活性和高效性。但我掌握和应用的还不是很熟练,应多加实践和练习,在以后的工作中,我将不断的学习和充实自己,力争成为一个高水平的程序员。
行业分析报告
mybatis-plus-extension.jar 各个版本,免费下载。 mybatis-plus 的扩展插件。,各个版本,免费下载。 mybatis 增强工具包的扩展插件,各个版本,免费下载。 下载不了,可关注我,评论区联系我。
halcon缺陷检测
1000+套最新计算机专业毕业设计源码+论文+PPT.txt.zip