首页 > 作文

实例详解SpringMVC入门使用

更新时间:2023-04-04 01:42:10 阅读: 评论:0

mvc模式(model-view-controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(model),视图(view)和控制器(controller).通过分层使开发的软件结构更清晰,从而达到开发效率的提高,可维护性和扩展性得到提高.spring提供的mvc框架是在j2ee web开发中对mvc模式的一个实现,本文通过实例讲解一下spring mvc 的使用.

先来看一个http request在spring的mvc框架是怎么被处理的:(图片来源于spring in action)

1,dispatcherrvlet是spring mvc的核心,它的本质是一个实现了j2ee标准中定义的httprvlet,通过在web.xml配置<rvlet-mapping>,来实现对request的监听.

<rvlet><rvlet-name>springtestrvlet</rvlet-name><rvlet-class>org.springframework.web.rvlet.dispatcherrvlet</rvlet-class><load-on-startup>2</load-on-startup></rvlet><rvlet-mapping><rvlet-name>springtestrvlet</rvlet-name><url-pattern>*.do</url-pattern></rvlet-mapping>

** 以.do结尾的request都会由springtestrvlet来处理.

2,3,当接受到request的时候,dispatcherrvlet根据handlermapping的配置(handlermapping的配置文件默认根据<rvlet-name>的值来决定,这里会读取springtes有什么单机游戏好玩的trvlet-rvlet.xml来获得handlermapping的配置信息),调用相应的controller来对request进行业务处理.

<bean id="simpleurlmapping" class="org.springframework.web.rvlet.handler.simpleurlhandlermapping"><property name="mappings"><props><prop key="/login.do">logincontroller</prop></props></property></bean><bean id="logincontroller" class="com.test.spring.mvc.contoller.logincontroller"><property name="ssionform"><value>true</value></property><property name="commandname"><value>logincommand</value></property><property name="commandclass"><value>com.test.spring.mvc.commands.logincommand</value></property><property name="authenticationrvice"><ref bean="authenticationrvice"/></property><property name="formview"><value>login</value></property><property name="successview"><value>logindetail</value></property></bean>

** 以login.do结尾的request由logincontroller来处理.<property name=”formview”>配置的是controller接收到http get请求的时候需要显示的逻辑视图名,本例是显示login.jsp,<property name=”successview”>配置的是在接收到http post请求的时候需要显示的逻辑视图名,在本例中即login.jsp提交的时候需要显示名为logindetail的逻辑视图.

4,controller进行业务处理之后,返回一个modelandview对象.

return new modelandview(getsuccessview(),"logindetail",logindetail);

5,6,dispatcherrvlet根据viewresolver的配置(本例是在springtestrvlet-rvlet.xml文件中配置)将逻辑view转换到真正要显示的view,如jsp等.

<bean id="viewresolver" class="org.springframework.web.rvlet.view.internalresourceviewresolver"><property name="viewclass"><value>org.springframework.web.rvlet.view.jstlview</value></property><property name="prefix"><value>/jsp/</value></property><property name="suffix"><value>.jsp</value></property></bean>

**其作用是将controller中返回的modleandview解析到具体的资源(jsp文件),如上例中的return new modelandview(getsuccessview();按照上面viewresolver配置,会解析成/jsp/logindetail.jsp.规则为prefix+modelandview的第二个参数+suffix.

示例的完整代码如下:

1web.xml:

<?xml version="1.0" encoding="utf-8"?><web-app id="webapp_id" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="/d/file/titlepic/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee /d/file/titlepic/ version="1.0" encoding="utf-8"?><beans xmlns="/d/file/titlepic/" xmlns:xsi="/d/file/titlepic/xmlschema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="simpleurlmapping" class="org.springframework.web.rvlet.handler.simpleurlhandlermapping"><property name="mappings"><props><prop key="/login.do">logincontroller</prop></props></property></bean><bean id="logincontroller" class="com.test.spring.mvc.contoller.logincontroller"><property name="ssionform"><value>true</value></property><property name="commandname"><value>logincommand</value></property><property name="commandclass"><value>com.test.spring.mvc.commands.logincommand</value></property><property name="authenticationrvice"><ref bean="authenticationrvice"/></property><property name="formview"><value>login</value></property><property name="successview"><value>logindetail</value></property></bean><bean id="viewresolver" class="org.springframework.web.rvlet.view.internalresourceviewresolver"><property name="viewclass"><value>org.springframework.web.rvlet.view.jstlview</value></property><property name="prefix"><value>/jsp/</value></property><property name="suffix"><value>.jsp</value></property></bean></beans>

3,springtest-rvices.xml的内容:

 <bean id="authenticationrvice" class="com.test.spring.mvc.rvices.authenticationrvice"/>

4,java代码:

public class logincontroller extends simpleformcontroller {org.springframework.web.rvlet.dispatcherrvlet t;protected modelandview onsubmit(object command) throws exception{logincommand logincommand = (logincommand) command;authenticationrvice.authenticate(logincommand);logindetail logindetail = authenticationrvice.getlogindetail(logincommand.geturid());return new modelandview(getsuccessview(),"logindetail",logindetail);}private authenticationrvice authenticationrvice;public authenticationrvice getauthenticationrvice() {return authenticationrvice;}public void tauthenticationrvice(authenticationrvice authenticationrvice) {this.authenticationrvice = authenticationrvice;}....public class logincommand {private string urid;private string password;....public class logindetail {private string urname;public class authenticationrvice {public void authenticate(logincommand command) throws exception{if(command.geturid()!= null && command.geturid().equalsignoreca("test") && command.getpassword()!= null && command.getpassword().equalsignoreca("test")){}el{throw new exception("ur id is not authenticated"); }}public logindetail getlogindetail(string urid){return new logindetail(urid);}}

5,jsp文件:放在web-inf的jsp文件夹内

login.jsp:<html><head><title>login to spring test</title></head><body&中秋节手抄报资料gt;<h1>plea enter your urid and password.</h1><form method="post" action="login.do"><table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5"><tr><td alignment="right" width="20%">ur id:</td><td width="20%"><input type="text" name="urid" value="test"></td><td width="60%"></tr><tr><td alignment="right" width="20%">password:</td><td width="20%"><input type="password" name="password" value="test"></td><td width="60%"></tr></table><br><input type="submit" alignment="center" value="login"></form></body></html> logindetail.jsp:<%@ taglib uri="/d/file/titlepic/" prefix="c"%> <html><head><title>login to spring test</title></head><body><h1>login details:</h1><br/>login as: <c:out value ="${logindetail.u南京金陵学院rname}"/><br/><a href="/s3/login.do" rel="external nofollow" >logout</a></body></html>

在浏览器的地址栏输入http://localhost:8080/xxx/login.do进入login.jsp页面.

对配置的一些扩充点:

1为handlermapping和controller的配置指定文件名称.

<rvlet>      <rvlet-name>springtestrvlet</rvlet-name>      <rvlet-class>org.springframework.web.rvlet.dispatcherrvlet</rvlet-class>      <init-param>          <param-name>contextconfiglocation</param-name>          <param-value>classpath*:/xxx.xml</param-value>      </init-param>      <load-on-startup>1</load-on-startup>  </rvlet>  

2,加入对mvc注解的支持:

<?xml version="1.0" encoding="utf-8"?><beans xmlns="/d/file/titlepic/"xmlns:xsi="/d/file/titlepic/xmlschema-instance" 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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><context:annotation-config /><context:component-scan ba-package="com.test.spring.mvc.contoller" /><mvc:annotation-driven /> @controllerpublic class annotationcontroller {    @requestmapping("annotation.do")    protected void test(httprvletrequest request,            httprvletrespon respon) throws exception{                respon.getwriter().println("test spring mvc annotation");    }

3,注解和simpleformcontroller同时使用需要在dispatcherrvlet对应的rvlet配置文件(本例是springtestrvlet-rvlet.xml)里面配置:

<bean class="org.springframework.web.rvlet.mvc.simplecontrollerhandleradapter"/> 否则会发生下面的异常:javax.rvlet.rvletexception: no adapter for handler [com.test.spring.mvc.contoller.logincontroller@6ac615]: does your handler implement a supported interface like controller?org.springframework.web.rvlet.dispatcherrvlet.gethandleradapter(dispatcherrvlet.java:982)org.springframework.web.rvlet.dispatcherrvlet.dodispatch(dispatcherrvlet.java:770)org.springframework.web.rvlet.dispatcherrvlet.dorvice(dispatcherrvlet.java:716)org.springframework.web.rvlet.frameworkrvlet.processrequest(frameworkrvlet.java:647)org.springframework.web.rvlet.frameworkrvlet.doget(frameworkrvlet.java:552)javax.rvlet.http.httprvlet.rvice(httprvlet.java:621)javax.rvlet.http.httprvlet.rvice(httprvlet.java:722)

在web应用中的context配置文件通过下面的方式load,contextloaderlistener是一个实现了rvletcontextlistener的listener,
它能够访问<context-param>而得到配置文件的路径,加载后初始化了一个webapplicationcontext,并且将其作为attribute放在了rvletcontext中,
所有可以访问rvletcontext的地方则可以访问w丁香结教案ebapplicationcontext.

<listener><listener-class>org.springframework.web.context.contextloaderlistener</listener-class></listener><context-param><param-name>contextconfiglocation</pa女子跆拳道ram-name><param-value>/web-inf/springtest-rvices.xml,classpath:conf/jndidsappcontext.xml</param-value></context-param>

到此这篇关于实例详解springmvc入门使用的文章就介绍到这了,更多相关springmvc详解内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

本文发布于:2023-04-04 01:41:56,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/f4f37f45a3f3455ea00293079e35d837.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:实例详解SpringMVC入门使用.doc

本文 PDF 下载地址:实例详解SpringMVC入门使用.pdf

标签:是在   视图   配置文件   本例
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图