BlogJava 联系 聚合 管理  

Blog Stats

随笔档案

文章档案

Infernu的Google site


Infernus-JXH

2009年11月3日 #

此文主要是实现不处理标签库的自定义标签,步骤如下:
首先写一个类,继承TagSupport,重写doStartTag(),doEndTag()如果要处理标签体,需要重写doAfterBody()。
public class MyFirstTag extends TagSupport {
    
private String format = "yyyy.MM.dd hh:mm:ss";

    @Override
    
public int doStartTag() throws JspException {
        JspWriter out 
= pageContext.getOut();
        SimpleDateFormat sdf 
= new SimpleDateFormat(format);
        
try {
            out.println(
"<table border='1'><tr><td bgcolor='blue'>");
            out.println(
"<font color='yellow' size='7'>");
            out.println(sdf.format(
new java.util.Date()));
            out.println(
"</font>");
            out.println(
"</td></tr></table>");
        }

        
catch (IOException e) {
            e.printStackTrace();
        }

        
return SKIP_BODY;    //跳过标签体[不需要处理标签的标签体]
    }

    
    
public void setFormat(String format){
        
this.format = format;
    }

    
}
其次,配置tld文件(标签库描述文件):

<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"><!-- 上面这段可以从JSTL里COPY -->
    
    
<description>My First Custom Tag Library</description><!-- 叙述 -->
    
<display-name></display-name><!-- 显示的名字 -->
    
<tlib-version>1.0</tlib-version><!-- 版本 -->
    
<short-name>MFCTL</short-name><!-- 短的名字 -->
    
<uri>cj</uri><!-- 全局唯一的uri,一般为"http:// ",此文只做示例,太短的话容易重复。与下文web.xml的配置应一致  -->
    
    
<tag>
        
<name>time</name><!-- 调用的名字,如<前缀:time/> -->
        
<tag-class>com.tsinghuait.tags.MyFirstTag</tag-class><!-- 具体的类 -->
        
<body-content>empty</body-content><!-- 标签体内容可以为空 -->
    
</tag>
    

然后,配置web.xml,告诉服务器有这么一个标签库。
    <jsp-config>
        
<taglib>
            
<taglib-uri>cj</taglib-uri><!-- 全局唯一的uri -->
            
<taglib-location>/WEB-INF/tld/Mytag.tld</taglib-location><!-- 标签库的描述文件位置 -->
        
</taglib>
    
</jsp-config>

最后,写一个页面用<%@taglib %>指令给定前缀和uri,调用标签。
<%@taglib prefix="lh" uri="cj"%>

<lh:time/>

posted @ 2009-11-27 10:11 Infernus 阅读(198) | 评论 (0)编辑 收藏

        作为服务器,安全是首要的问题。在WebRoot下页面用户可以直接输入地址访问,而每个页面都配置安全选项又太不现实。而在Tomcat有一个安全的目录,直接拒绝用户使用地址栏访问,那就是WebRoot\WEB-INF。然而在此目录下的页面又不能直接进行跳转,默认的跳转目录是WebRoot。这时我们就需要写导航,具体方法如下:
        这是一个网页登陆页面index.jsp:        

<center>
    
<form action="/review/login" method="post"><!-- 提交到的页面不能直接写成/WEB-INF/login.jsp或者login.jsp,要写成"工程/配置的name" -->
            
<table border="0" cellspacing="20">
            
<tr>
                
<td>
.
                
</td>
            
</tr>
        
</table>
    
</form>
    
</center>
然后在WEB-INF/web.xml下配置如下:
<servlet>
        
<servlet-name>login</servlet-name><!-- name -->
        
<jsp-file>/WEB-INF/login.jsp</jsp-file><!-- 具体的文件地址 -->
    
</servlet>
    
    
<servlet-mapping>
        
<servlet-name>login</servlet-name>
        
<url-pattern>/login</url-pattern><!-- 地址栏显示的URL -->
    
</servlet-mapping>
posted @ 2009-11-27 09:39 Infernus 阅读(2075) | 评论 (0)编辑 收藏

1.手动设置数据源:
<%@ page pageEncoding="GBK"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/sql" prefix="sql" %>

<!-- 手动设置数据源 dataSource="url, Driver, username, password"-->
<sql:setDataSource dataSource=
    "jdbc:mysql://localhost:3306/myweb, com.mysql.jdbc.Driver, root, lovejack"
 
    var
="myds" scope="application"/>

<!-- 查询代码 -->
<sql:query var="items" dataSource="${myds}" 
    sql
="SELECT * FROM t_items where price <= ? and price >= ?">
    
<sql:param value="20"/>
    
<sql:param value="10"/>
</sql:query>

<!-- 制表显示-->
<table border="1">
    
<tr>
        
<th>商品名称</th>
        
<th>商品价格</th>
    
</tr>
    
<c:forEach var="row" items="${items.rows}">
        
<tr>
            
<td><c:out value="${row.name}"/></td>
            
<td><c:out value="${row.price}"/></td>
        
</tr>
    
</c:forEach>
</table>
 2.在web.xml文件中配置了数据源:
<%@page pageEncoding="GBK"%>
<%@taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jstl/sql" prefix="sql" %>

<!-- 在web.xml文件中配置了数据源 -->
<sql:query var="items">
    SELECT * FROM t_items
</sql:query>

<table border="1">
    
<tr>
        
<th>商品名称</th>
        
<th>商品价格</th>
    
</tr>
    
<c:forEach var="row" items="${items.rows}">
        
<tr>
            
<td><c:out value="${row.name}"/></td>
            
<td><c:out value="${row.price}"/></td>
        
</tr>
    
</c:forEach>
</table>

    WEB-INF/web.xml配置如下:
     <context-param>
        
<param-name>javax.servlet.jsp.jstl.sql.dataSource</param-name>
        
<param-value>jdbc:mysql://192.168.0.13:3306/myweb,
              com.mysql.jdbc.Driver, root, lovejack
</param-value>
    
</context-param>
    
3.访问Tomcat中配置的连接池数据源:
<%@page pageEncoding="GBK"%>
<%@taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jstl/sql" prefix="sql" %>

<!-- 访问Tomcat中配置的连接池数据源 -->
<sql:query var="items" dataSource="jdbc/myds">
    SELECT * FROM t_items
</sql:query>

<table border="1">
    
<tr>
        
<th>商品名称</th>
        
<th>商品价格</th>
    
</tr>
    
<c:forEach var="row" items="${items.rows}">
        
<tr>
            
<td><c:out value="${row.name}"/></td>
            
<td><c:out value="${row.price}"/></td>
        
</tr>
    
</c:forEach>
</table>
需要在Tomcat/conf/tomcat-users.xml里配置管理员:
<role rolename="admin"/>
<role rolename="manager"/>

 
<user username="jackfrued" password="lovejack" roles="admin,manager"/>
再需要在localhost/admin中使用上面的用户名,密码登录 。配置当前工工程文件的Data Source。
把url,Driver,username,password都写入Tomcat的连接池中。
posted @ 2009-11-06 14:57 Infernus 阅读(415) | 评论 (0)编辑 收藏

使用EL自定义函数的四个步骤:
1. 创建一个公开类,写公开的静态的方法;
public class MyFunctions {

    
public static String reverse(String s) {
        String rs 
= "";
        
for(int i = s.length() - 1; i >= 0 ; i--{
            rs 
+= s.charAt(i);
        }

        
return rs;
    }

    
    
public static double jc(int n) {
        
double result = 1.0;
        
for(int i = n; i > 1; i--{
            result 
= result * i;
        }

        
return result;
    }

}

2. 通过标签库表述文件(*.tld)描述表达式语言中要使用的函数(<function>--><name>/<function-class>/<function-signature>) // <description>hahaha...</description>为在Eclipse里的提示。
<function>
        
<description>hahaha</description>
        
<name>reverse</name>
        
<function-class>com.tsinghuait.beans.MyFunctions</function-class>
        
<function-signature>java.lang.String reverse(java.lang.String)</function-signature>
    
</function>
    
    
<function>
        
<name>jc</name>
        
<function-class>com.tsinghuait.beans.MyFunctions</function-class>
        
<function-signature>double jc(int)</function-signature>
    
</function>

3. 修改web.xml文件,通过<jsp-config>--><taglib>标签对标签库进行声明(<taglib-uri>/<taglib-location>)
  <jsp-config>
      
<taglib>
          
<taglib-uri>http://www.tsinghuait.com/el</taglib-uri>
          
<taglib-location>/WEB-INF/tld/myel.tld</taglib-location>
      
</taglib>
  
</jsp-config>

4. 在JSP页面中使用taglib指令表明使用标签库:<%@taglib prefix="..." uri="..."%>
                                                                                        ${前缀:函数名(参数列表)}
<%@page isELIgnored="false" pageEncoding="GBK"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:out value="Hello, world!"></c:out><br/>
<c:out value="${123 * 456}"></c:out><br/>


posted @ 2009-11-03 17:57 Infernus 阅读(325) | 评论 (0)编辑 收藏

能在jsp页面中更简单的拿取参数。注释掉的为以前的代码
<%--
    String s 
= request.getParameter("i");
    
int i = Integer.parseInt(s);
    out.println(i 
* 8);
--%>

    $
{param.i * 8}<br/><br/>
Scope未指定位置的取值方向为 page -> request -> session -> application  如果没有找到,不会影响其它信息。
<%
    request.setAttribute(
"sss""Goodbye, world!");
    session.setAttribute(
"sss""Hello, world!");
    session.setAttribute(
"kkk"100);
    application.setAttribute(
"kkk"200);
%>


$
{sss} ${kkk} <br/><br/>
$
{sessionScope.sss}  ${applicationScope.kkk}<br/><br/>
格式
${sessionScope.stu.name} ${stu.age} ${stu["sex"]}<%-- 有特殊符号需要[]--%>

posted @ 2009-11-03 17:49 Infernus 阅读(170) | 评论 (0)编辑 收藏