SpringMVC入门实例

2025-05-27 0 103

1介绍

mvc框架是什么

mvc全名是model view controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。mvc被独特的发展起来用于映射传统的输入、处理和输出功能在一个逻辑的图形化用户界面的结构中。

模型-视图-控制器(mvc)是一个众所周知的以设计界面应用程序为基础的设计模式。它主要通过分离模型、视图及控制器在应用程序中的角色将业务逻辑从界面中解耦。通常,模型负责封装应用程序数据在视图层展示。视图仅仅只是展示这些数据,不包含任何业务逻辑。控制器负责接收来自用户的请求,并调用后台服务(manager或者dao)来处理业务逻辑。处理后,后台业务层可能会返回了一些数据在视图层展示。控制器收集这些数据及准备模型在视图层展示。mvc模式的核心思想是将业务逻辑从界面中分离出来,允许它们单独改变而不会相互影响。

SpringMVC入门实例

在springmvc应用程序中,模型通常由pojo对象组成,它在业务层中被处理,在持久层中被持久化。视图通常是用jsp标准标签库(jstl)编写的jsp模板。控制器部分是由dispatcherservlet负责,在本教程中我们将会了解更多它的相关细节。

一些开发人员认为业务层和dao层类是mvc模型组件的一部分。我对此持有不同的意见。我不认为业务层及dao层类为mvc框架的一部分。通常一个web应用是3层架构,即数据-业务-表示。mvc实际上是表示层的一部分。

SpringMVC入门实例

dispatcher servlet(spring控制器)

在最简单的spring mvc应用程序中,控制器是唯一的你需要在java web部署描述文件(即web.xml文件)中配置的servlet。spring mvc控制器 ——通常称作dispatcher servlet,实现了前端控制器设计模式。并且每个web请求必须通过它以便它能够管理整个请求的生命周期。

当一个web请求发送到spring mvc应用程序,dispatcher servlet首先接收请求。然后它组织那些在spring web应用程序上下文配置的(例如实际请求处理控制器和视图解析器)或者使用注解配置的组件,所有的这些都需要处理该请求。

SpringMVC入门实例

在spring3.0中定义一个控制器类,这个类必须标有@controller注解。当有@controller注解的控制器收到一个请求时,它会寻找一个合适的handler方法去处理这个请求。这就需要控制器通过一个或多个handler映射去把每个请求映射到handler方法。为了这样做,一个控制器类的方法需要被@requestmapping注解装饰,使它们成为handler方法。

handler方法处理完请求后,它把控制权委托给视图名与handler方法返回值相同的视图。为了提供一个灵活的方法,一个handler方法的返回值并不代表一个视图的实现而是一个逻辑视图,即没有任何文件扩展名。你可以将这些逻辑视图映射到正确的实现,并将这些实现写入到上下文文件,这样你就可以轻松的更改视图层代码甚至不用修改请求handler类的代码。

为一个逻辑名称匹配正确的文件是视图解析器的责任。一旦控制器类已将一个视图名称解析到一个视图实现。它会根据视图实现的设计来渲染对应对象。

2导入jar包

至少应该有这些.

SpringMVC入门实例

3 配置文件

3.1 web.xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36
<?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" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="webapp_id" version="3.0">

<display-name>springmvc_helloworld</display-name>

<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>

<!-- spring mvc 的servlet -->

<!-- dispatcherservlet在初始化后会直接在/web-inf/下找springmvc-servlet.xml文件,

servlet-name标签的参数定义要和xml文件对应 -->

<servlet>

<servlet-name>springmvc</servlet-name>

<servlet-class>

org.springframework.web.servlet.dispatcherservlet

</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>springmvc</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

<context-param>

<param-name>contextconfiglocation</param-name>

<param-value>classpath:applicationcontext.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.contextloaderlistener</listener-class>

</listener>

</web-app>

3.2 springmvc-servlet.xml

这个文件的名字是由web.xml里面配置的dispatcherservlet的<servlet-name></servlet-name>决定的,路径在上下文/web-inf/里面,主要是配置控制器返回的逻辑视图名和物理视图的对应关系

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26
<?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:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"

xsi:schemalocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- 自动扫描的包 -->

<context:component-scan base-package="com.lin.helloworld.controller" />

<bean id="viewresolver"

class="org.springframework.web.servlet.view.urlbasedviewresolver">

<property name="viewclass"

value="org.springframework.web.servlet.view.jstlview" />

<!-- controller 返回的一个逻辑视图名经过前后缀的处理返回物理视图 -->

<property name="prefix" value="/web-inf/jsp/" />

<property name="suffix" value=".jsp" />

</bean>

</beans>

4 编写一个domain类

用来封装一些提交数据

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21
package com.lin.helloworld.domain;

public class helloworld {

private string data;

public helloworld() {

super();

}

public helloworld(string data) {

super();

this.data = data;

}

public string getdata() {

return data;

}

public void setdata(string data) {

this.data = data;

}

@override

public string tostring() {

return "helloworld [data=" + data + "]";

}

}

5 编写controller

这个是mvc中的控制器,和struts2不一样的是他是方法级的拦截,struts2是类级的拦截.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32
package com.lin.helloworld.controller;

import org.springframework.stereotype.controller;

import org.springframework.web.bind.annotation.modelattribute;

import org.springframework.web.bind.annotation.requestmapping;

import org.springframework.web.servlet.modelandview;

import com.lin.helloworld.domain.helloworld;

@controller

public class helloworldcontroller {

//这里的/hello相当于struts2里的一个action

//返回一个字符串给视图

@requestmapping("/hello")

public modelandview sayhello()

{

//modelandview的构造方法的第一个参数相当于struts2里的一个result的name

modelandview modelandview = new modelandview("helloworld", "msg", "helloworld!!!");

return modelandview;

}

//返回一个对象给视图

//@modelattribute("obj")的作用相当于struts2的action类里面的一个field,

//用于表单提交的数据放进一个对象里面

//这里和struts2的区别:

//struts2处理表单提交的方式是:<input name="obj.data"/> 提交的数据封装在obj对象的data里面

//springmvc的方式是:<input name="data"/> 提交的数据封装在obj对象的data里面,

//前提是要使用@modelattribute注解

@requestmapping("/helloobj")

public modelandview sayhelloworld(@modelattribute("obj") helloworld obj)

{

system.out.println(obj.tostring());

modelandview modelandview = new modelandview("helloworld", "obj", obj);

return modelandview;

}

}

6 视图

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>

<%

string path = request.getcontextpath();

string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";

%>

<!doctype html public "-//w3c//dtd html 4.01 transitional//en">

<html>

<head>

<base href="<%=basepath%>" rel="external nofollow" >

<title>my jsp 'helloworld.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="this is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" >

-->

</head>

<body>

helloworld! this is a spring mvc framework example.<br>

${msg}

<hr/>

<form action="helloobj" method="post">

<!-- 这里的表单提交和struts2不同的是name="data"会自动对应上对象的filed -->

<input type="text" name="data" size="30"/><br/>

<input type="submit" value="submit"/>

</form>

<hr/>

${obj.data}

</body>

</html>

7 目录结构

SpringMVC入门实例

总结

以上就是本文关于springmvc入门实例的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。感谢朋友们对本站的支持。

原文链接:http://www.open-open.com/code/view/1454080281355

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 SpringMVC入门实例 https://www.kuaiidc.com/77353.html

相关文章

发表评论
暂无评论