JSP æ ç¾è¿æ¯å¾æ¹ä¾¿çï¼æ¯å¦ StrutsãSpring çæä¾ç»æ们ç JSP æ ç¾ï¼å¯ä»¥ç¨å®ä»¬æ¥è·ååéæè¿è¡ä¸äºè®¡ç®ãæ¯å¦
struts2 ç <s:url value="/admin/unmi.action"/>
ä¼ä¸ºæ们èªå¨å¨åé¢éå ä¸åºç¨ä¸ä¸æï¼å¦å®é
ä¼çæ
/testsite/admin/unmi.actionãè¿ææ´å¤æ´æ¹ä¾¿çæ ç¾ä½¿ç¨ï¼æ¯å¦ç¨æ ç¾è·åå° session æ请æ±ä¸çæ°æ®ä½ä¸º js
çåéçã
å¼ç³æ¤è¯é¢çï¼å
¶å®ä¸å
æ¯å¨ JS ä¸ä½¿ç¨ JSP æ ç¾ï¼å¯ç¨ JSP æ ç¾çå°æ¹å½ç¶å¯ä»¥ç´æ¥å Java 代ç ï¼å³ ScriptLet 代ç ã
å¦ææ¯å¨ JSP ä¸å
åµç JS 代ç ï¼é£å½ç¶å¥½åï¼JSP æ件ä¸è½ç¨ä»ä¹æ ç¾ï¼js 代ç ä¸ä¹è½é便ç¨ï¼å 为 JSP æ ç¾ä¼å
å¨æå¡ç«¯è§£éçæç¸åºç JS 代ç ï¼ä¸¢ç»å®¢æ·ç«¯æ§è¡ã
é£å¦ææ¯å¨åç¬ç JS æ件ä¸æ³è¦ä½¿ç¨ JSP æ ç¾ï¼è¯¥å¦ä½åå¢ï¼è¿ç´æ¥å JSP é£æ ·ä½¿ç¨æ ç¾çè¯ï¼å¯¹ä¸èµ· JS
æ件ä¸ç»ä½ åæ ·æ¾ç¤ºåºæ¥ï¼å 为 JS æ件ä¸è¢«æå¡å¨ç«¯è§£éãå
¶å®å°ç°å¨é®é¢ä¹åºæ¬æäºçæ¡ï¼è¦è§£å³çé®é¢å°±æ¯è¦è®©æå¡ç«¯å»è§£éä½ çæ ç¾ï¼æ两ç§æ¹æ¡ã
ä¸. js æ件å½å为 JSP æ件ï¼å js å
容ï¼å
¶ä¸ç¨æ ç¾ï¼ç¶åç¨ <script src="/scripts/tags.js.jsp"></script> æ该 JSP æ件å½ä½ JS æ件é£æ ·å¼å
¥ã
å 为 JSP æ件ä¼è¢«æå¡ç«¯è§£éï¼æ以æ该 JSP æ件å½ä½ JS æ件æ¥åï¼JS æ件éåºè¯¥æ¯ä»ä¹ï¼ä½ ç JSP
æ件ä¹åºè¯¥è¾åºä»ä¹ï¼åªæ¯éé¢å¯ä»¥æ¾æ ç¾ï¼è¦ç¥éå®å°ä¼è¾åºä»ä¹ãç¨ <script> æ¥å¼å
¥çè¯ï¼é¡µé¢é¡ºççæå®è®¤ä¸ºæ¯ä¸æ®µå¤é¨ JS
代ç ã
ä¾å¦æ件 /scripts/tags.js.jsp ä¸çå
容æ¯ï¼
<%@ taglib prefix="s" uri="/struts-tags" %>
var currentUser = '<s:property value="#session.userName" />';
alert("currentUser: " + currentUser);
é£ä¹å¨æ个ç½é¡µä¸ç¨ <script src="/scripts/tags.js.jsp"></script> å¼å
¥è¯¥æ件æ¶ï¼ä¸é¢ç JS 代ç å°ä¼å¼¹åºå½å session ä¸çç¨æ·åæ¥ã
ä¸é¢ä»£ç æ§è¡é½æ²¡é®é¢çï¼ä½æ¯ä½ ç´æ¥æµè§
http://unmi/testsite/scripts/tags.js.jsp ä½ çå°çæ¯æ¤å¨ä¸å¢ç代ç ï¼ä¸åæ¥çæ®é JS æ件é£æ ·ææ¸
æ°çæ¢è¡åéæ ¼ï¼åå æ¯æµè§å¨é»è®¤åªè®¤æ©å±åï¼å®ç mime ç±»åï¼ä¹å°±æ¯ Content-Type text/htmlãè¦è®©å®æ´åæ¯ä¸ª JS æ件é£å¾ç»å®å ä¸ååºç±»åç设置ï¼å¨ tags.js.jsp æ件第ä¸è¡å ä¸ï¼
<%response.setContentType("text/javascript;charset=utf-8");%>
è¿æ¶å对äºæµè§å¨æ¥è¯´ï¼æ 论ä»åªä¸ªæ¹åæ¥çå®é½æ¯ä¸ªååå®å®ç JS æ件ï¼åªæ©å±åä¸åç½¢äºã
ä¸é¢çæ¹æ³å®é
表ç°æ¯æ²¡ä»ä¹é®é¢çï¼ä¸è¿è¿æ两ç¹ä¸é£ä¹å®ç¾ï¼ç¬¬ä¸ï¼æ件åçèµ·æ¥å JSP æ件ï¼æäºä»¤äººè¯¯è§£ï¼ç¬¬äºï¼å¨ IDE ä¸æå¼è¯¥ JSP æ件ï¼æ²¡æ³åºç¨ JS çè¯æ³å 亮ï¼ç»ç¼è¾å¸¦æ¥ä¸å°éº»ç¦ãä¸é¢çæ¹æ³å
å解å³åé¢ä¸¤ä¸ªé®é¢ã
äº. ç´æ¥å¨ JS æ件ä¸ä½¿ç¨ JSP æ ç¾
讲ä¸åçï¼ä¸æ¯è¯´åºç¨æå¡å¨ç«¯é»è®¤ä¸è§£é JS ä¸çæ ç¾åï¼é£æ们å¯ä»¥è®©ä¸ªå«ç JS æ件åæ ·åå°æå¡å¨ç«¯çå
³æ³¨ï¼ä¸æ¯æ js åæç´æ¥æç»å®¢æ·ç«¯ï¼èæ¯å
解éå
¶ä¸çæ ç¾ï¼ææ¯å
¶ä¸ç java 代ç ã
Tomcat è¿ç§åºç¨æå¡å¨æ们é常ä¹ä¼ç§°å®ä¸º Servlet 容å¨ï¼å 为å®æ§è¡çæ¯ Servletï¼JSP èªç¶ä¹æ¯ Servletãå¨ %TOMCAT_HOME%/conf/web.xml ä¸æ们å¯ä»¥çå°å®é
å¤ç JSP æ件ç Servlet æ¯ï¼
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>fork</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>xpoweredBy</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>
org.apache.jasper.servlet.JspServletï¼æ以æ们å¯ä»¥å¨èªå·±åºç¨ç web.xml
æ件ä¸é
ç½®æäºç¹å«çæ件åæ ·ç± JspServlet æ¥å¤çãæ¯å¦è¦ç¹å«å¤ç /scripts/tags.js æ件ï¼å¨åºç¨ç web.xml
ä¸åªè¦å ä¸ï¼
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>/scripts/tags.js</url-pattern>
</servlet-mapping>
注æï¼ä¸é¢æ¯ Tomcat 6.x ææ´æ©çä¸åºç¨ç web.xml çåæ³ï¼åæ¥ servlet-mapping ä¸
url-pattern å¯åæ¶åå¤ä¸ªï¼å ä¸ Tomcat 7 èªèº«çå¤æªï¼è¿ä¸è®¾ç½®ä¼è¦çæå jsp ç设置ï¼æä»¥å¨ Tomcat 7
ä¸åºç¨å¿
é¡»é
ç½®æå¦ä¸ï¼ä¹å°±æ¯å¿
é¡»é»è®¤é¡¹ä¹å¸¦ä¸ï¼ç¶èååºèªå·±çå¸æ被å½ä½ jsp ç js æ件ï¼å¯å¤ä¸ªï¼ä¸æ *.jsp/*.jspx
带ä¸çè¯ï¼ä½ åæç jsp ä¼å½ä½ææ¬æ件ç´æ¥å±ç¤ºåºæºç æ¥ï¼ï¼
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.jspx</url-pattern>
<url-pattern>/scripts/tags.js</url-pattern>
</servlet-mapping>
æ们é
ç½® /scripts/tags.js è¦ç± JspServlet æ¥å¤çï¼ä¸ä¼å½±åå°ç°æçå
¶ä»ä»»ä½ JS æ件ã
é£ç°å¨ç /scripts/tags.js å¯ä¸æ¯æ®éç js æ件äºï¼å®å¯æ¯å
·ææ¾ç½® JSP æ ç¾åå Java
代ç çè¶
è½åäºï¼å 为å
¶ä¸ç JSP æ ç¾å Java 代ç é¦å
ä¼ç»ç±æå¡ç«¯æ¥è§£éãå®é JS å JSP äºä¸èº«ï¼å¦å¤ä¹å«å¿äºç»è¯¥ JS
æ件åé¢å ä¸ä»£ç ï¼
<%response.setContentType("text/javascript;charset=utf-8");%>
ä¸ç¶åç¬æµè§å®ä¹å°±åªæ¯ä¸é£ä¹å¥½çã
æ çï¼è¿æ¯ç®åæè½æ³çæå®ç¾çä¸ç§æ¹å¼äºï¼ä¸å
¼å®¹äºå
¶ä»ç Servlet 容å¨ï¼ä¹ä¸ç¨æ¹å¨å
Œ
±é¨åçä¸è¥¿ã/scripts/tags.js å¨ JS IDE ä¸æå¼ä¹æ¼äº®å¤äºï¼å 为å®å°±æ¯ä¸ª JS æ件ï¼åªæ¯è¢«èµäºäº JSP çåè½ã