实例详解SpringMVC入门使用
mvc模式(model-view-controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(model),视图(view)和控制器(controller).通过分层使开发的软件结构更清晰,从而达到开发效率的提高,可维护性和扩展性得到提高.spring提供的mvc框架是在j2ee web开发中对mvc模式的一个实现,本文通过实例讲解一下spring mvc 的使用.
先来看一个http request在spring的mvc框架是怎么被处理的:(图片来源于spring in action)
1,dispatcherservlet是spring mvc的核心,它的本质是一个实现了j2ee标准中定义的httpservlet,通过在web.xml配置<servlet-mapping>,来实现对request的监听.
<servlet> <servlet-name>springtestservlet</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springtestservlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
** 以.do结尾的request都会由springtestservlet来处理.
2,3,当接受到request的时候,dispatcherservlet根据handlermapping的配置(handlermapping的配置文件默认根据<servlet-name>的值来决定,这里会读取springtestservlet-servlet.xml来获得handlermapping的配置信息),调用相应的controller来对request进行业务处理.
<bean id="simpleurlmapping" class="org.springframework.web.servlet.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="sessionform"> <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="authenticationservice"> <ref bean="authenticationservice"/> </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,dispatcherservlet根据viewresolver的配置(本例是在springtestservlet-servlet.xml文件中配置)将逻辑view转换到真正要显示的view,如jsp等.
<bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="viewclass"> <value>org.springframework.web.servlet.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="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name> prjspring3</display-name> <servlet> <servlet-name>springtestservlet</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springtestservlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.requestcontextlistener</listener-class> </listener> <context-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/springtest-services.xml</param-value> </context-param> <jsp-config> <taglib> <taglib-uri>/spring</taglib-uri> <taglib-location>/web-inf/spring.tld</taglib-location> </taglib> </jsp-config> </web-app>
2,springtestservlet-servlet.xml的内容如下:
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/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.servlet.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="sessionform"> <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="authenticationservice"> <ref bean="authenticationservice"/> </property> <property name="formview"> <value>login</value> </property> <property name="successview"> <value>logindetail</value> </property> </bean> <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="viewclass"> <value>org.springframework.web.servlet.view.jstlview</value> </property> <property name="prefix"> <value>/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>
3,springtest-services.xml的内容:
<bean id="authenticationservice" class="com.test.spring.mvc.services.authenticationservice"/>
4,java代码:
public class logincontroller extends simpleformcontroller { org.springframework.web.servlet.dispatcherservlet t; protected modelandview onsubmit(object command) throws exception{ logincommand logincommand = (logincommand) command; authenticationservice.authenticate(logincommand); logindetail logindetail = authenticationservice.getlogindetail(logincommand.getuserid()); return new modelandview(getsuccessview(),"logindetail",logindetail); } private authenticationservice authenticationservice; public authenticationservice getauthenticationservice() { return authenticationservice; } public void setauthenticationservice( authenticationservice authenticationservice) { this.authenticationservice = authenticationservice; } .... public class logincommand { private string userid; private string password; .... public class logindetail { private string username; public class authenticationservice { public void authenticate(logincommand command) throws exception{ if(command.getuserid()!= null && command.getuserid().equalsignorecase("test") && command.getpassword()!= null && command.getpassword().equalsignorecase("test")){ }else{ throw new exception("user id is not authenticated"); } } public logindetail getlogindetail(string userid){ return new logindetail(userid); } }
5,jsp文件:放在web-inf的jsp文件夹内
login.jsp: <html><head> <title>login to spring test</title></head> <body> <h1>please enter your userid 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%">user id:</td> <td width="20%"><input type="text" name="userid" 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="http://java.sun.com/jstl/core_rt" prefix="c"%> <html><head> <title>login to spring test</title></head> <body> <h1>login details:</h1> <br/> login as: <c:out value ="${logindetail.username}"/> <br/> <a href="/s3/login.do" rel="external nofollow" >logout</a> </body> </html>
在浏览器的地址栏输入http://localhost:8080/xxx/login.do进入login.jsp页面.
对配置的一些扩充点:
1为handlermapping和controller的配置指定文件名称.
<servlet> <servlet-name>springtestservlet</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>classpath*:/xxx.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
2,加入对mvc注解的支持:
<?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: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/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"> <context:annotation-config /> <context:component-scan base-package="com.test.spring.mvc.contoller" /> <mvc:annotation-driven /> @controller public class annotationcontroller { @requestmapping("annotation.do") protected void test(httpservletrequest request, httpservletresponse response) throws exception{ response.getwriter().println("test spring mvc annotation"); }
3,注解和simpleformcontroller同时使用需要在dispatcherservlet对应的servlet配置文件(本例是springtestservlet-servlet.xml)里面配置:
<bean class="org.springframework.web.servlet.mvc.simplecontrollerhandleradapter"/> 否则会发生下面的异常: javax.servlet.servletexception: no adapter for handler [com.test.spring.mvc.contoller.logincontroller@6ac615]: does your handler implement a supported interface like controller? org.springframework.web.servlet.dispatcherservlet.gethandleradapter(dispatcherservlet.java:982) org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:770) org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:716) org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:647) org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:552) javax.servlet.http.httpservlet.service(httpservlet.java:621) javax.servlet.http.httpservlet.service(httpservlet.java:722)
在web应用中的context配置文件通过下面的方式load,contextloaderlistener是一个实现了servletcontextlistener的listener,
它能够访问<context-param>而得到配置文件的路径,加载后初始化了一个webapplicationcontext,并且将其作为attribute放在了servletcontext中,
所有可以访问servletcontext的地方则可以访问webapplicationcontext.
<listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <context-param> <param-name>contextconfiglocation</param-name> <param-value> /web-inf/springtest-services.xml, classpath:conf/jndidsappcontext.xml </param-value> </context-param>
到此这篇关于实例详解springmvc入门使用的文章就介绍到这了,更多相关springmvc详解内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!