|
1 <bean id="helloWorld" class="org.andyny.action.HelloWorld"> <constructor-arg index="0"> //注意: index="引号+数字" <value>hi,hahahaha</value> </constructor-arg> </bean>
2
spring配置文件 applicationContext.xml配置文件放在本项目的工作目录下,即是:springMVC应用程序下。或者Web项目的WEB-INF目录下
3
web.xml,config.xml等各配置文件中,若批处理命令中参数(如:web-app 2.2/web-app 2.4版本不匹配问题及encoding编码与平台的默认编码不符合问题) 都会显示错误。
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
4
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered? at org.springframework.web.servlet.support.RequestContextUtils.getWebApplicationContext(RequestC o ntextUtils.java:84) at org.springframework.web.servlet.support.RequestContext.initContext(RequestContext.java:206) at org.springframework.web.servlet.support.JspAwareRequestContext.initContext(JspAwareRequestCon t ............
解决办法: <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/applicationContext.xml </param-value> </context-param>
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
将上述的代码添加到web.xml文件,注意是:<servlet>标签之前。
5 配置taglib
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app 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" version="2.4">
<jsp-config> // 注意:此版本需要<jsp-config>, web-app_2_2.xsd则不要。 <taglib> <taglib-uri> /spring </taglib-uri> <taglib-location> /WEB-INF/spring.tld </taglib-location> </taglib> </jsp-config>
6
ListIterator 注意:void remove() 从迭代器指向的 collection 中移除迭代器返回的最后一个元素(可选操作)。
Iterator和ListIterator的不同使用方法
我们在使用List,Set的时候,为了实现对其数据的遍历,我们经常使用到了Iterator(跌代器)。使用跌代器,你不 需要干涉其遍历的过程,只需要每次取出一个你想要的数据进行处理就可以了。
但是在使用的时候也是有不同的。List和Set都有iterator()来取得其迭代器。对List来说,你也可以通过 listIterator()取得其迭代器,两种迭代器在有些时候是不能通用的,Iterator和ListIterator主要区别在以下方 面:
1. ListIterator有add()方法,可以向List中添加对象,而Iterator不能
2. ListIterator和Iterator都有hasNext()和next()方法,可以实现顺序向后遍历,但是ListIterator有 hasPrevious()和previous()方法,可以实现逆向(顺序向前)遍历。Iterator就不可以。
3. ListIterator可以定位当前的索引位置,nextIndex()和previousIndex()可以实现。Iterator没有此功能。
4. 都可实现删除对象,但是ListIterator可以实现对象的修改,set()方法可以实现。Iierator仅能遍历,不能修 改。
因为ListIterator的这些功能,可以实现对LinkedList等List数据结构的操作。其实,数组对象也可以用迭代器来 实现。
org.apache.commons.collections.iterators.ArrayIterator就可以实现此功能。一般情况下,我们使用Iterator 就可以了,如果你需要进行记录的前后反复检索的话,你就可以使用ListIterator来扩展你的功能,(有点象JDBC 中的滚动结果集)。
7 Spring MVC中关于数据绑定功能:
++++++++++++++login.jsp+++++++++++++++++
<form name="User" action="/spring/login.do" method="post"> <spring:bind path="command.username"> <spring:message code="username"/><input type="text" name="${status.expression}" value="${status.value}"/><br> <font color="red"><b>${status.errorMessage}</b></font><br> </spring:bind>
+++++++++++++applicationContext.xml++++++++++++++
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="login.do">loginAtion</prop> </props> </property> </bean>
+++++++++++++++++Action类的配置代码片段+++++++++++++++
<bean id="loginAtion" class="com.gc.action.Login"> <property name="commandClass"> //++++++++++++注入command类,即是form表单的映射对象Bean <value>com.gc.action.User</value> </property> <property name="validator"> <ref bean="userValidator"/> </property> <property name="formView"> <value>login</value> </property> <property name="successView"> <value>success</value> </property> </bean>
那么必须从Action类中定向到login.jsp,而不能直接打开login.jsp页面。 否则, <spring:bind path="command.username"> 标签中的command对象取不到, 会报如下错误: Neither Errors instance nor plain target object for bean name 'command' available as request attribute
8 关于web.xml文件中配置 dispatcherServlet-servlet.xml
++++++++++++++++++++++++++++++++方式一+++++++++++++++++++++++++++++++++
注意:此中方式中,param-name一定要是contextConfigLocation,而方式二则没有什么限制。
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/dispatcherServlet-servlet.xml </param-value> </context-param>
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
<servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
+++++++++++++++++++++++++方式2+++++++++++++++++++++++
<servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcherServlet-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
<servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|