1.web.xml加载顺序
http://mianhuaman.iteye.com/blog/1105522
关键点:ServletContext -> context-param -> listener -> filter -> servlet
2.beans xmlns
http://www.w3school.com.cn/xml/xml_namespaces.asp
用于避免元素命名冲突。在Spring中应用极其广泛。
3.SpringMVC
http://blog.jobbole.com/29706/
优点是层次清晰,适配灵活。更可以基于注解支持零配置。
几个重要的标签 http://honphenc.blog.163.com/blog/static/18961734520123226151941/
<!-- 启用spring mvc 注解 --> <context:annotation-config /> <!-- 扫描加载指定的包中有相关注解的类 --> <context:component-scan base-package="com.controller"></context:component-scan> <mvc:annotation-driven /> <!-- 相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean,配置一些messageconverter。即解决了@Controller注解的使用前提配置。 -->
4.ServletRequest + ServletResponse(实际使用的是HttpServletRequest + HttpServletResponse)
request
http://baike.baidu.com/view/4668529.htm
方法 | 作用 |
public String getParameter(String name) | 获取request中的参数 |
public BufferedReader getReader() throws IOException | 获得request的正文(字符流) |
public ServletInputStream getInputStream() | 获得request的正文(二进制流) |
response
http://hi.baidu.com/javalovers/item/730686f94028ec6c3d14857d
http://docs.oracle.com/javaee/6/api/javax/servlet/ServletResponse.html
方法 | 作用 |
response.setCharacterEncoding("UTF-8"); | 只是将数据用UTF-8编码 |
response.setHeader("Content-Type","text/html;charset-UTF-8"); | 告诉客户端编码方式 |
response.setContentType("text/html;charset=UTF-8"); | 功效等同于以上两行代码 |
6.莫名其妙乱码问题(这里的情景使用的编码是UTF-8)
(1)指定读取POST时的编码:在web.xml中配置EncodingFilter
(2)指定读取GET时的编码:以tomcat为例,在server.xml中设置
<Connector port="80"
protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"
URIEncoding="UTF-8"/>
(3)单元测试时无法配置EncodingFilter,尤其用@RequestBody和@ResponseBody自动转换时:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <util:list id="beanList"><ref bean="stringHttpMessageConverter" /></util:list> </property> </bean> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean>
(4)Eclipse控制台乱码:Window | Preferences | Workspace | Text file encoding | UTF-8