要求:     1.画面A属性对应数据库B字段。
     2.画面上要显示数据库B字段的内容.     如果:A属性值为:abc。
           数据库B字段值为:AbcAAAabcZZtabcDD          显示结果:AbcAAAabcZZtabcDD

解决方案 »

  1.   

    在后台用String replace(CharSequence target,CharSequence replacement) 
    直接把A字段替换成 <html>A <html/>(具体html代码忘了怎么写了) 
    然后传到前台,差不多可以解决
      

  2.   

    典型的正则表达式需求:
    A属性为匹配模式,
    B字段为被匹配的字符串,
    匹配结果加上html标签直接展示在DIV即可。
    public static void main(String[] args) throws Exception { 
          String strB="AbcAAAabcZZtabcDD";
          String strA="abc";
          Pattern p=Pattern.compile(strA);
          Matcher m=p.matcher(strB);
          StringBuffer sb=new StringBuffer();
          while(m.find()){
          m.appendReplacement(sb, "<fond color='red'>"+m.group()+"</font>");
          }
          m.appendTail(sb);
          
          String htmlCode=sb.toString();
          System.out.println(htmlCode);
        } 
      

  3.   

    如果数据量比较少,前端替换也比较方便// 在串s中标记关键字k
    function sk(s,k){
      return s.replace(new RegExp(k,'g'),'<b>'+k+'</b>');
    }
      

  4.   

    直接用replaceAll
    String strB="AbcAAAabcZZtabcDD";
            String strA="abc";
            strB=strB.replaceAll(strA, "<fond color='red'>"+strA+"</font>");
            System.out.println(strB);
      

  5.   

    就是加亮算法呗。 replace是最简单的,也是效率很不错的。 不要用replaceAll, 那个是正则的,速度慢。
      

  6.   

    交给客户端去做吧,这样耦合性比较小一些。<%@ page contentType="text/html; charset=gbk"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><style type="text/css">
    #content em {
      color: red;
      font-style:normal;
    }
    </style>
    <body>  
      <div id="content">汉字是中华文明的瑰宝,汉字是中华文明的瑰宝,汉字是中华文明的瑰宝</div>  
    </body>
    </html><script type="text/javascript">
    window.$ = function(id) {
      if(typeof id == 'string') {
        return document.getElementById(id);
      }
      return id;
    }highlight('content');function highlight(id) {  
      var s = [<c:forEach items="${keywords}" var="key" varStatus="status">'${key}'<c:if test="${not status.last}">,</c:if></c:forEach>];
      var regex = new RegExp(s.join('|'), 'g');
      $(id).innerHTML = $(id).innerHTML.replace(regex, function(a) {
            return '<em>' + a + '</em>';
        });
    }
    </script>import java.io.IOException;import javax.servlet.Servlet;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;public class TestServlet extends HttpServlet implements Servlet {    private static final long serialVersionUID = -7122921551754535982L;     public TestServlet() {
            super();
        }    
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doPost(request, response);
        }
        
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            String[] keywords = { "汉字", "瑰宝", "文明" };
            request.setAttribute("keywords", keywords);
            forward("/test.jsp", request, response);
        }
        
        private void forward(String path, HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            getServletContext().getRequestDispatcher(path).forward(request, response);
        }
    }