易截截图软件、单文件、免安装、纯绿色、仅160KB

JSP(JSTL)中使用常量防止硬编码

this.state="01";通常的做法是写一个类(接口)来存放常量
public interface MyConstant
{
public static final String STATE_01= "01";
}
然后在程序中这样写就可以了
this.state=MyConstant.STATE_01;
在Java程序中这样就可以避免硬编码了。可是JSP中呢?当然,如果JSP中允许使用Scriplet的话当然也可以直接使用常量了,不过现在JSP中一般不允许出现<%%>这样的代码,比如在JSTL中怎么办呢?
<c:if test=${state=='01'}>
</c:if>
这样又出现了'01'这样的硬编码了,该如何使用常量类中的变量呢?
下面介绍用JspTag的方式来处理:
首先在/WEB-INF/tags/下建立my-tag.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>my tag</description>
    <display-name>my tag</display-name>
    <tlib-version>1.0</tlib-version>
    <short-name>my-tag</short-name>
    <uri>/my-tag</uri>
    <tag>
    <!--
        在JSP中初始化MyConstant常量,以便JSTL中引用。
        jsp中使用范例:<my-tag:MyConstant var="常量名称"/>
        必填参数var:指定初始化MyConstant中某个变量,如果为空,则报异常
        可选参数scope:变量作用范围,默认为page
   -->
        <description>MyConstant常量</description>
        <name>MyConstant</name>
        <tag-class>com.test.util.tag.MyConstantTag</tag-class>
        <body-content>JSP</body-co


相关文档:

jsp和数据库连接大全

下面的jsp和数据库连接大全,请参考
一、jsp连接Oracle8/8i/9i数据库(用thin模式)
testoracle.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Stri ......

JSP考点总结

1,JSP中动态INCLUDE与静态INCLUDE的区别?
动态INCLUDE用jsp:include动作实现 <jsp:include page="included.jsp" flush="true" />它总是会检查所含文件中的变化,适合用于包含动态页面,并且可以带参数。    
####这个是把引用的jsp编译成servlet以后,直接调用servlet类的做法。####
静态INCLUDE用inc ......

JSP基础语法大全

注释
在客户端显示一个注释.
JSP 语法
<!-- comment [ <%= expression %> ] -->
例子 1
<!-- This file displays the user login screen -->
在客户端的HTML源代码中产生和上面一样的数据:
<!-- This file displays the user login screen -->
例子 2
<!-- This page was lo ......

JSP标签

一 。自定义JSP标签的处理过程:
  1.在JSP中引入标签库:
  2.在JSP中使用标签库标签
  3.Web容器根据第二个步骤中的prefix,获得第一个步骤中声明的taglib的uri属性值
  4.Web容器根据uri属性在web.xml找到对应的元素
    5.从元素中获得对应的元素的值
    6.Web容 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号