发表于: 2017-03-02 23:26:19

2 1667


今天完成的事情:学习自定义标签,将页面使用tiles替换
明天计划的事情:了解cookie,了解DES算法,学习MD5加密

遇到的问题:1.从请求urlurl—>controller—>JSP requestresponse是怎么判断,有时候就分不清楚了
                     2.使用tiles框架的时候,切换变的模块,比如body,body的样式用什么方式处理会好点(我暂时是又新建了一个模块,相当于每次切换body时也切换样式)

收获:自定义标签:使用自定义标签的话,一般都是直接使用继承SimpleTagSupport,实现

public void doTag() throws JspException, IOException {}

这个方法,这个方法就能完成所有的业务逻辑,重写dotag方法

1.直接输出标签的内容:

//得到代表jsp标签体的JspFragment
JspFragment jspFragment = this.getJspBody();

//将标签体的内容输出到浏览器
jspFragment.invoke(null);

2.处理标签的内容然后输出:

//得到代表jsp标签体的JspFragment
JspFragment jspFragment = this.getJspBody();
StringWriter sw = new StringWriter();
//将标签体的内容写到sw流中
jspFragment.invoke(sw);
//获取sw流缓冲区的内容
String content = sw.getBuffer().toString();
content = content.toUpperCase();
PageContext pageContext = (PageContext) jspFragment.getJspContext();
//将修改后的content输出到浏览器中
pageContext.getOut().write(content);

3.给标签添加属性

private int count;
public void setCount(int count) {
this.count = count;
@Override
public void doTag() throws JspException, IOException {
for (int i = 0; i < count; i ++){
this.getJspBody().invoke(null);
   }
}

然后在WEB-INF/目录下新建tld文件,在tld文件中对标签处理器类进行描述

<?xml version="1.0" encoding="UTF-8" ?>
<taglib 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-jsptaglibrary_2_0.xsd"
       version="2.0">
   <!--description用来添加对taglib(标签库)的描述-->
   <description>自定义标签库</description>
   <!--taglib的版本号-->
   <tlib-version>1.0</tlib-version>
   <short-name>TagLibrary</short-name>
   <!--
       为自定义标签库设置一个uri,uri以/开头,/后面的内容随便写,如这里的/viewIP,
       在JSP页面中引用标签库时,需要通过uri找到标签库
       在Jsp页面中就要这样引入标签库:<%@taglib uri="/viewIP" prefix="tagTest"%>
   -->
   <uri>/testTag</uri>
   <!--一个taglib中包含多个自定义标签,每个自定义标签使用一个tag标记来描述-->
   <!--一个tag标记对应一个自定义标签-->
   <tag>
       <description>这个标签的作用是用来输出客户端的IP地址</description>
       <!--
           为标签处理器类配一个标签名,在Jsp页面中使用标签时是通过标签名来找到要调用的标签处理器类
           通过对应的viewIP就能找到对应的ViewIPTag类
       -->
       <name>viewIP</name>
       <tag-class>com.ptteng.tag.ViewIPTag</tag-class>
       <body-content>empty</body-content>

       <!--<%@taglib uri="标签库的uri"  prefix="标签的使用前缀"%>-->
   </tag>

   <tag>
       <name>demo1</name>
       <tag-class>com.ptteng.tag.DateTransformTag</tag-class>
       <body-content>JSP</body-content>

   </tag>

</taglib>

在JSP中添加

<%@taglib uri="标签库的uri"  prefix="标签的使用前缀"%>
使用taglib指令引用viewIP标签库,标签库的(prefix)可以随便设置
<%@taglib prefix="lt" uri="/testTag" %>

如果标签有属性

<tag>
   <name>simpleTag4</name>
   <tag-class>com.ptteng.tag.SimpleTagDemo4</tag-class>
   <body-content>scriptless</body-content>
   <!--标签的属性描述-->
   <attribute>
       <!--标签的count属性-->
       <name>count</name>
       <required>true</required>
       <!--
           rtexprvalue用来指示标签的属性值是否可以是一个表达式
           一般设置为true,true就表示允许标签的属性值可以是表达式
       -->
       <rtexprvalue>true</rtexprvalue>
   </attribute>
</tag>

在JSP中:就可以给属性赋值

<lt:simpleTag4 count="2"><h1>老大最帅</h1></lt:simpleTag4>

使用简单标签的时候细节注意问题:

在简单标签中:
  • body-content的值只允许empty,scriptless,tagdependent,不允许设置成JSP
  • body-content的值如果设置成empty,那么就表示该标签没有标签体,如果是设置成scriptless,那么就表示该标签是有标签体的,但是标签体重的内容不可以是<%Java代码%>
 tld文件中用于描述标签属性的<attribute>元素说明
  • <tag>元素的<attribute>子元素用于描述自定义标签的一个属性,自定义标签所具有的每个属性都要对应一个<attribute>元素


Tiles总结:

首先tiles就是将JSP分成一块一块的,然后进行模块化,对应大量相似的页面就可以把变化的拿出来保留不变的模块。

首先是添加依赖:

<dependency>
 <groupId>org.apache.tiles</groupId>
 <artifactId>tiles-api</artifactId>
 <version>3.0.7</version>
</dependency>
<dependency>
 <groupId>org.apache.tiles</groupId>
 <artifactId>tiles-core</artifactId>
 <version>3.0.7</version>
</dependency>
<dependency>
 <groupId>org.apache.tiles</groupId>
 <artifactId>tiles-jsp</artifactId>
 <version>3.0.7</version>
</dependency>
<dependency>
 <groupId>org.apache.tiles</groupId>
 <artifactId>tiles-servlet</artifactId>
 <version>3.0.7</version>
</dependency>
<dependency>
 <groupId>org.apache.tiles</groupId>
 <artifactId>tiles-template</artifactId>
 <version>3.0.7</version>
</dependency>

然后创建模块对应的JSP:

配置tiles的xml配置文件,

这个相当于模板:template.jsp是模板,里面插入title,header,body,footer四个模块

<definition name="base.definition" template="/WEB-INF/layout/template.jsp">

   <put-attribute name="title" value="" />
   <put-attribute name="header" value="/WEB-INF/layout/header.jsp"/>
   <put-attribute name="body" value=""/>
   <put-attribute name="footer" value="/WEB-INF/layout/footer.jsp"/>
</definition>

header和footer是不变的,然后创建继承模板的字模板:

填充title和body

<definition name="home" extends="base.definition">
   <put-attribute name="title" value="/WEB-INF/home-head.jsp"/>
   <put-attribute name="body" value="/WEB-INF/home.jsp"/>
</definition>

模板JSP,就是相当于把其他模块放进来

<%@taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
   <tiles:insertAttribute name="title"/>
</head>
<body>
   <tiles:insertAttribute name="header"/>
<tiles:insertAttribute name="body"/>
<tiles:insertAttribute name="footer"/>
</body>
</html>


然后在springmvc的配置文件中添加bean:

<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" p:order="1"/>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
   <property name="definitions">
       <list>
           <value>/WEB-INF/tiles.xml</value>
       </list>
   </property>
</bean>

注:这儿出现一个问题,添加tiles,相当于添加了一个视图解析器,这样两个解析器同时同时指向一个/home 时就会报错,这时候就需要给解析器添加优先级:

  • 1.视图解析器顺序,order 越小,优先级越高    
  • 2.多个视图解析器,会按顺序解析视图,解析不成功会交给下一个解析器,最好把jsp解析器放到最后
  • 3.多个InternalResourceViewResolver 会报404





返回列表 返回列表
评论

    分享到