When the request leaves the browser, it carries information about what the user is asking for. At very least, the request will be carrying the requested URL. But it may also carry additional data such as the information submitted in a form by the user.
The first stop in the request’s travels is Spring’s DispatcherServlet . Like most Java-based MVC frameworks, Spring MVC funnels requests through a single front controller servlet. A front controller
is a common web-application pattern where a single servlet delegates responsibility for a request to other components of an application to perform the actual processing. In the case of Spring MVC, DispatcherServlet is the front controller.
The DispatcherServlet’s job is to send the request on to a Spring MVC controller. A controller is a Spring component that processes the request. But a typical application may have several controllers and DispatcherServlet needs help deciding which controller to send the request to. So, the DispatcherServlet consults one or more handler mappings C to figure out where the request’s next stop will be. The handler mapping will pay particular attention to the URL carried by the request when making its decision.
Once an appropriate controller has been chosen, DispatcherServlet sends the request on its merry way to the chosen controller. D At the controller, the request will drop off its payload (the
information submitted by the user) and patiently wait for the controller to process that information. (Actually, a well-designed Controller performs little or no processing itself and instead delegates responsibility for the business logic to one or more service
objects.)
The logic performed by a controller often results in some information that needs to be carried back to the user and displayed in the browser. This information is referred to as the model. But sending
raw information back to the user isn’t sufficient—it needs to be formatted in a user-friendly format, typically HTML. For that the information needs to be given to a view, typically a JSP.
So, the last thing that the controller will do is package up the model data and the name of a view into a ModelAndView object. It then sends the request, along with its new ModelAndView parcel,
back to the DispatcherServlet. As its name implies, the ModelAndView object contains both the model data as well as a hint to what view should render the results.
So that the controller isn’t coupled to a particular view, the ModelAndView doesn’t carry a reference to the actual JSP. Instead it only carries a logical name that will be used to look up
the actual view that will produce the resulting HTML. Once the ModelAndView is delivered to the DispatcherServlet, the DispatcherServlet asks a view resolver to help find the actual JSP.
Now that the DispatcherServlet knows which view will render the results, the request’s job is almost over. Its final stop is at the view implementation (probably a JSP) where it delivers the
model data. With the model data delivered to the view, the request’s job is done. The view will use the model data to render a page that will be carried back to the browser by the (not-so-hard-working) response object.
摘自《 Manning Spring in action》
通过对SpringMVC中request生命周期的理解。我们就很容易记住如何来配置他了。
核心标签库
它是JSTL中的核心库,为日常任务提供通用支持,如显示和设置变量、重复使用一组项目、测试条件和其他操作(如导入和重定向Web内容)。Core标签按功能可分为4种类型:
1 变量维护:
(1)<c:set>:设置变量值和对象属性。语法如下:
<c:set value="值" var="变量名" scope="变量的作用域" target="对象名" property=" 对象属性名"></c:set>
每种设置都有两种方式,总结起来,<c:set>的4种形式,如下所示:
a. 使用标记属性设置JSP变量
<c:set value="值" var="变量名" scope="作用域"/>
b. 使用标记体设置JSP变量
<c:set var="变量名" scope="作用域">标记内容</c:set>
c. 使用标记属性设置对象属性
<c:set value="变量名" target="对象名" property="对象属性名"/>
d. 使用标记体设置对象属性
<c:set target="对象名" property="作用域">标记内容</set>
(2)<c:remove>:在指定作用域范围内删除变量。语法如下:
<c:remove var="变量名" scope="作用域"/>
2 流程控制:分为条件标签和迭代标签。
条件标签:<c:if> <c:choose> <c:when> <c:otherwise>
(1)<c:if>:与Java语言中的if语句的使用方法一样,但不能实现else的功能。
<c:if>标签有两种语法形式,是以有无标记体来区分的。
无标签体:
<c:if test="测试条件" var="变量名" [scope="作用域"]/>
有标签体:
<c:if test="测试条件" var="变量名" [scope="作用域"]>
标签体
</c:if>
带标记体的<c:if>
<c:if test="${user.visitCount!=0}">欢迎光临</c:if>
(2)<c:choose> <c:when> <c:otherwise>
<c:when> <c:otherwise>无法单独使用,只能作为<c:choose>的子标签来使用。这三个标签组合起来实现Java中的switch语句的功能。语法如下:
<c:choose>
<c:when test="${user.class==’guest’}">
标签体1
</c:when>
<c:when test="${user.class==’vip’}">
标签体2
</c:when>
<c:otherwise>
标签体3
</c:otherwise>
</c:choose>
迭代标签:<c:forEach> <c:forTokens>
(1)<c:forEach>:用于遍历一个对象集合。
<c:forEach var="变量名" items="集合" varStatus="遍历状态名"
begin="begin" end="end" step="step" >
标签体
</c:forEach>
(2)<c:forTokens>:用于遍历字符串,而且每次遍历结果返回字符串中的一个单词。
<c:forTokens items="字符串" delims="分界符" var="变量名"
varStatus="遍历状态名" begin="begin" end="end" step="sep">
标签体
</c:forTokens>
3 URL管理
(1)<c:url>:用于对URL地址进行编码。
有标签体:
<c:url value="URL" context="路径" var="变量名" scope="作用域">
标签体
</c:url>
如下代码:
<c:url value="http://localhost:8080/el/index.jsp" var="NewURL">
<c:param name="name" value="zero"/>
<c:param name="age" value="28"/>
</c:url>
<a href=/blog_article/"${NewURL}">点我呀</a>_br/index.html>
生成的URL:http://localhost:8080/el/index.jsp?name=zero&age=28
无标签体:主要用于编辑上下文URL。
<c:url value="URL" context="路径" var="变量名" scope="作用域"/>
如下代码:
<c:url value="/logon.jsp">登录</c:url>
若当前路径为el,则输出为:/el/logon.jsp
(2)<c:import>:向当前JSP页面中引入URL资源(可以是远程序站点上的资源)。Include指令和include动作不能向JSP页引入Web程序以外的资源,引入的资源必须位于当前Web程序中。
以String对象引入的语法:
<c:import url="地址" context="上下文路径" var="变量名"
scope="作用域" charEncoding="字符集">
标签体使用<c:param>
</c:import>
如下代码:将外部资源引入到当前JSP页面中.
<c:import url="http://www.hao123.com" var="myurl" charEncoding="gb2312">
</c:import>
<a href=/blog_article/"${myurl }">地址</a>_br/index.html>
以Reader对象导入的语法:
<c:import url="地址" context="上下文路径" varReader="变量名"
scope="作用域" charEncoding="字符集">
标签体使用其它动作元素
</c:import>
(3)<c:redirect>:用于HTTP重定向。
无标签体:
<c:redirect url="地址" context="上下文路径"/>
有标签体:
<c:redirect url="地址" context="上下文路径">
<c:param/>标签
</c:redirect>
(4)<c:param>:只能嵌入到<c:url>、、<c:import>、<c:redirect>标签中作为子元素来使用。此标签主要用于设置URL中将要传入的参数。
无标签体:
<c:param name="名称" value="值"/ >
有标签体:
<c:param name="名称" value="值" >
标签体
</c:param>
4 其它标签:<c:out>、<c:catch>。
(1)<c:out>:在JSP页面中显示变量内容。
无标签体:
<c:out value="值" escapeXml="{true|false}" default="默认值"/>
有标签体:
<c:out value="值" escapeXml="{true|false}" default="默认值">
标签体
</c:out>
其中:
default: 用于指定当value值为null时,应该输出的值。
escapeXml: 用于设置是否将"<"、">"、"&"、"’"、"""、这些字符进行转义。
escapeXml默认为true,表示发生转换。
"<"转换成"<"
">"转换成">"
"&"转换成"&"
"’"转换成"'"
"""转换成"""
(2)<c:catch>:用于处
mysql> SHOW VARIABLES LIKE 'character%';
+----------------------------------------+-------------------------
| Variable_name |Value
+----------------------------------------+-------------------------
| character_set_client | latin1
|character_set_connection | latin1
|character_set_database | utf8
| character_set_filesystem | binary
| character_set_results |latin1
| character_set_server |utf8
| character_set_system |utf8
| character_sets_dir |D:\mysql\share\charsets\
+----------------------------------------+-------------------------
8 rows in set (0.00 sec)
mysql>SHOW VARIABLES LIKE 'collation_%';
+---------------------------------------+------------------
| Variable_name |Value
+---------------------------------------+------------------
| collation_connection |latin1_swedish_ci
| collation_database |utf8_general_ci
| collation_server |utf8_general_ci
+--------------------------------------+------------------
3 rows in set (0.00 sec)
从中我们可以看出,整个编码中,不全是utf8编码的。这样就会造成一些问题,例如:我们抓取的中文是utf8编码的,然后传给mysql,但是mysql的connection和server都是latin1编码的,而表示utf8,于是提交的utf8中文内容,会先被latin1编码,然后再由utf8编码。如果这样的话,再用第三方软件访问数据库就会出现问题,例如用phpadmin等,访问的虽然是utf8内容,但是是由latin1编码过后的utf8字符,不免会出现问题。
解决的方式:当然就是编码统一,全部采用utf8编码方式。如何更改?这里只介绍mysql5.5 以上的修改方式,我的mysql是5.5.29的。修改方式如下:
打开mysql配置文件:sudovim /etc/mysql/my.cnf
[mysqld]