欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java代码段+jsp的基础应用

程序员文章站 2022-06-09 12:25:57
...

项目需要大量使用自定义标签,讨厌的<%%>和<%=-%>,加上html+js+jquery

那么好吧,玩吧! 记下来,给自己个备忘。

 

1.先上个基础的,复习一下

  注意:定义的变量,可以直接在js中使用,当做参数或值。这很重要,即传来的参数就可以在这直接用到js中

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<%
	String name = "mengxianjun";
	String inputReadonly = "readonly='true'";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <input type="text" id="test" value="" onblur="checkInput<%=name%>()" <%=inputReadonly%>/>
  </body>

<script type="text/javascript">
<%
	if(name!=null){
%>
       alert('name不为空:'+'<%=name%>');
<%
	}
%>
	var myName = '<%=name%>';
	document.getElementById('test').value=myName;
	
	function checkInput<%=name%>()
	{
		alert('input 鼠标离开事件');
	}
</script>
</html>

    

2.自定义标签,怎么使用(PSAreaTag.tag)

<%@ tag pageEncoding="UTF-8" import="java.util.*,com.hanpeng.base.util.SelectManagerRequest" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ attribute name="id"  required="true" rtexprvalue="true"%>
<%@ attribute name="name"  required="true" rtexprvalue="true"%>

<%@ attribute name="pid"  required="false" rtexprvalue="true"%>
<%@ attribute name="action" required="false" rtexprvalue="true"%>

<%@ attribute name="isSelect"   required="false" rtexprvalue="true" %> <!--  -->
<%@ attribute name="isShowHead"   required="false" rtexprvalue="true" type="java.lang.Boolean"%> <!-- 是否显示请选择 -->
<%@ attribute name="headinfo"   required="false" rtexprvalue="true" %> <!-- 自定义下拉框默认显示 -->
<%@ attribute name="sendMethod"   required="false" rtexprvalue="true" %> <!-- 调用参数 -->
<%@ attribute name="disabledstate"   required="false" rtexprvalue="true" %> <!-- 可传任意值  如:disabled=true -->
<%@ attribute name="widlength"   required="false" rtexprvalue="true" %> <!-- 设置宽度 -->
<%@ attribute name="diyClass"   required="false" rtexprvalue="true" %> <!-- 自定义样式 -->
<%
	List list = null;
	if(pid == null && sendMethod != null){
		if("areaLinkMenus".equals(sendMethod)) list = SelectManagerRequest.getAreaProvince(request);/* 省市联动 */
		if("inOrOutProvince".equals(sendMethod)) list = SelectManagerRequest.getInOrOutProvince(request);/* 省市联动 */
	}
%>

<select id="<%=id %>" name="<%=name %>"  class="<%=diyClass %>" 
		<%=disabledstate %> 
		style="<%=widlength %>">
	<%     if(isShowHead != null && isShowHead == true){   %>
	<%         if(headinfo != null){ %>
					<option value="" selected ><%=headinfo %></option>
	<%         }else{                %>
					<option value="" selected >请选择</option>
	<%         } 					 %>
	<%     }                         %>
	<% if(list != null && !list.isEmpty()){             %>
	<%     for (int i=0;i<list.size();i++ ){
		      java.util.HashMap ot = (java.util.HashMap)list.get(i);
		      String oti = ot.get("val").toString();
		      String selected = "";
		      if(oti.equals(isSelect)){
			      selected = "selected";
		      }
	%>
		      <option value="<%=ot.get("val")%>" name="<%=ot.get("key") %>" &nbsp; <%=selected%> ><%=ot.get("key")%></option>
	<%     }   %>
	<% }       %>
</select>
<%if(pid != null && !pid.equals("") && action != null && !action.equals("")){ %>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/doubleCombo/doubleCombo.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  new DoubleCombo("<%=pid %>","<%=id %>","<%=action %>",{
	errorHandler:function(request){
		alert("网络交互出错");
	}
	<%     if(isShowHead != null && isShowHead == true){   %>
	<%         if(headinfo != null){ %>
					,head: '<%=headinfo %>'
	<%         }else{                %>
					,head: '请选择'
	<%         } 					 %>
	<%     }                         %>
	<%     if(isSelect != null){     %>
					,defaultChild: '<%=isSelect %>'
	<%     }                         %>
	}).masterComboChanged();
});
</script>
<%} %>