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 语法
<!-- comment [ <%= expression %> ] -->
例子 1
<!-- This file displays the user login screen -->
在客户端的HTML源代码中产生和上面一样的数据:
<!-- This file displays the user login screen -->
例子 2
<!-- This page was lo ......
在编写JSP程序时,常常会碰到中文字符处理的问题,在接受request的中文字符时显示出来一串乱码。网上处理方法一箩筐,下面说说我用过的两种有效地解决办法:
1.为程序编写一个字符串处理函数,用一个静态文件保存,在需要处理中文字符的JSP页面中包含它,
<%!
public String codeToString(String str)
{ ......
1. RequestDispatcher.forward()
是在服务器端起作用,当使用forward()时,Servlet engine传递HTTP请求从当前的Servlet or JSP到另外一个Servlet,JSP 或普通HTML文件,也即你的form提交至a.jsp,在a.jsp用到了forward()重定向至b.jsp,此时form提交的所有信息在 b.jsp都可以获得,参数自动传递. 但fo ......
解决jsp或serverlet 不能解析multipart/form-data 类型的表单域的问题
10 6月 2008
Situation:
Javax.servlet.HttpServletRequest.getParameter(String) returns null when the ContentType is multipart/form-data
Solutions:
Solution A:
1. download http://www.servlets.com/cos/index.html
2. invoke getPara ......
复选框的使用及JSP对数据的处理
<!--
Description: HTML表单复选框及JSP处理测试
Author: pxzl
Date: 2009-08-05 15:12:43
-->
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String[] cheArray1=request.getParameterValues("chkbox1");
String[] cheArray2=req ......