<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} a:link, span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:1777405401; mso-list-type:hybrid; mso-list-template-ids:-825730764 1704071866 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l0:level1 {mso-level-text:%1、; mso-level-tab-stop:18.0pt; mso-level-number-position:left; margin-left:18.0pt; text-indent:-18.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} -->
SpringMVC的配置过程,程序中具体的一些原理我还没有搞太明白,继续学习中,先把成功的案例的配置过程总结一下
1、 新建一个 web 工程,这个就不多说了
2、 和其他的 java web 一样,首先来配置一个 web.xml
Web.xml:
<? xml version = "1.0" encoding = "UTF-8" ?>
< web-app xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version = "2.5" >
< servlet >
<!-- 通过此处的命名, spring 会自动去 WEB-INF/ 下寻找 此名 + -servlet.xml 文档 如此处,我用 spring 做为名字,则它会自动去找匹配的 WEB-INF/spring-servlet.xml 文档 -->
< servlet-name > spring </ servlet-name >
< servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class >
< load-on-startup > 2 </ load-on-startup >
</ servlet >
<!-- 配置一个请求后缀,凡是以 do 结尾的路径,都会被 springmvc 拦截 -->
< servlet-mapping >
< servlet-name > spring </ servlet-name >
< url-pattern > *.do </ url-pattern >
</ servlet-mapping >
<!--Spring ApplicationContext 载入 -->
< listener >
< listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
</ listener >
<!-- Character Encoding filter -->
< filter >
< filter-name > setCharacterEncoding </ filter-name >
< filter-class > org.springframework.web.filter.CharacterEncodingFilter </ filter-class >
< init-param >
< param-name > encoding </ param-name >
< param-value > UTF-8 </ param-value >
</ init-param >
</ filter >
< filter-mapping >
< filter-name > setCharacterEncoding </ filter-name >
< url-pattern > /* </ url-pattern >
</ filter-mapping >
< welcome-file-list >
< welcome-file > /WEB-INF/jsp /index.jsp </ welcome-file >
</ welcome-file-list >
</ web-app >
这里我们主要配置了一个 DispatcherServlet 和访问的 url (详细内容看上面的注释)。和其他的 spring 的配置不一样的是,我们不需要配置 <context-param> ,和创建 applicationContext.xml 文件, Spring 会自动去 WEB-INF 下寻找 Servlet 名 +servlet.xml 文件进行处理。
3、 接下来自然是配置 spring-servlet.xml 文件
Spring-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.xsd " >
< bean id = "urlMapping" class = "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
< property name = "mappings" >
< props >
< prop key = "login.do" > login </ prop >
< prop key = "index.do" > index </ prop >
</ props >
</ property >
</ bean >
< bean id = "login" class = "cn.oyangk.web.action.LoginCtrl" >
< property name = "commandClass" value = "cn.oyangk.web.model.User" ></ property >
</ bean >
< bean id = "index" class = "cn.oyangk.web.action.IndexCtrl" >
</ bean >
</beans>
这个里面主要是一些 bean ,意思也不太难懂。在 <bean id=”login”> 中配置了一个 property, 这个主要是用来获取页面表单提交的值,直接绑定到 User 类中,这样就不需要用 request.getparameter( “” ) 来一个一个的获取页面的值。
4、 下面主要是 LoginCtrl.java 类
public class LoginCtrl extends AbstractCommandController {
@Override
protected ModelAndView handle(HttpServletRequest request,
HttpServletResponse response, Object obj, BindException exception)
throws Exception {
System. out .println(request.getParameter ( "username" ));
User u = (User)obj;
Map map = new HashMap ();
map.put( "bean" , u) ;
if (u.getUsername().equals( "tiger" ) && u.getPassword().equals( "123" ))
{
return new ModelAndView( "/WEB-INF/jsp/main.jsp" ,map);
}
return new ModelAndView( "/WEB-INF/jsp/failed.jsp" ,map);
}
代码中定义的 map 主要是为了在 main.jsp 和 failed.jsp 页面访问的到 u 中的 username 和 password 。
最后想把源码传上来的,可是不知道怎么弄