Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java EE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 解決JSP程式撰寫不直覺、程式碼與UI混雜的痛: JSPWidget (0.9.4 版新增加 action 的功能)
lee5518





发贴: 4
积分: 0
于 2003-07-11 16:00 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
相关话题: http://www.cjsdn.com/post/view?bid=2&id=37650

JSPWidget 0.9.4 版新增加了 action 的功能了唷
之前在 try 這個 Tag Library 時
發現蠻好用的
可是後來發現在 sbumit 時
不能自己指定 action 的值
也就是由 servlet 或是別的 JSP 來接收
現在新版在 form 裡面增加了 action 功能
下面是我使用簡單的 MVC 所做的測試
我使用的 OS 是 winxp
JDK 的版本 j2sdk1.4.2
湯姆貓(Tomcat)的版本 tomcat-4.1.24

web.xml 的部分設定
<web-app>
.
.
.
<servlet>
<servlet-name>JackyTest</servlet-name>
<servlet-class>jacky.servlet.JackyTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JackyTest</servlet-name>
<url-pattern>/JackyTest</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>/jsp/index.htm</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/jspwidget</taglib-uri>
<taglib-location>/WEB-INF/jspwidget.tld</taglib-location>
</taglib>
.
.
.
</web-app>

在湯姆貓(Tomcat)實際檔案的路徑
JSP
C:\Program Files\Apache Group\Tomcat 4.1\webapps\jacky\jsp
servlet
C:\Program Files\Apache Group\Tomcat 4.1\webapps\jacky\WEB-INF\classes\jacky\servlet
classes
C:\Program Files\Apache Group\Tomcat 4.1\webapps\jacky\WEB-INF\classes\jacky\util

總共有 2 個 JSP 的程式和 1 個 servlet 的程式和 1 個 JAVA 的程式
1. jacky_10.jsp (輸入的頁面有包含宣告式欄位編審(Declarative Validation))
2. jacky_11.jsp (從 session 中抓出要呈現的值)
3. JackyTest.class (servlet 的程式)
4. JackyUtil.class (JAVA 的程式)

jacky_10.jsp :
<%@ page contentType="text/html;charset=big5" session="true" import="com.ucom.dao.sql.*,com.ucom.util.*,java.util.*,com.ucom.jsp.tags.gui.*,com.ucom.jsp.tags.validation.*,com.ucom.jsp.tags.sql.*,com.ucom.jsp.tags.util.*" %>
<%@ taglib uri="/jspwidget" prefix="jacky" %>
<HTML>
<HEAD>
<TITLE></TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=big5" />
</HEAD>
<BODY>
<jacky:form id="jspForm" action="/jacky/JackyTest">
<h4>requiredFieldValidator Widget</h4>
<table cellpadding=0 border=1>
<tr valign="middle">
<td>頁面驗證結果:</td>
<td>
<span id="lblOutput" style="color:Red;font-size:14pt;">請填寫以下欄位</span>
</td>
</tr>
<tr>
<td colspan=2 align=center><b>個人資訊</b></td>
</tr>
<tr>
<td align=right><font size=2 color=red>姓名:</font></td>
<td>
<jacky:textBox id="requiredFieldTextBox1"/>
<jacky:requiredFieldValidator id="TextBox1_val1"
controlToValidate="requiredFieldTextBox1"
     display="Static"
onMouseOver="this.style.backgroundColor='lightblue' "
onMouseOut="this.style.backgroundColor='white' "
    errorMessage="*:不可空白" />

<tr>
<td align=right><font size=2 color=red>教育程度:</font></td>
<td>
<jacky:dropdownList id="requiredFieldDropdownList1" >
<jacky:listItem text="" value="" />
<jacky:listItem text="研究所以上" value="01" />
<jacky:listItem text="大學" value="02" />
<jacky:listItem text="專科" value="03" />
<jacky:listItem text="高中職" value="04" />
<jacky:listItem text="其他" value="05" />
</jacky:dropdownList>
<jacky:requiredFieldValidator id="DropdownList1_val1"
  controlToValidate="requiredFieldDropdownList1"
  errorMessage="*您總有個教育程度吧?" />
</td>
</tr>
<tr>
<td></td>
<td>
<jacky:button id="btnSbumit" text="送出" causeValidation="true" />
</td>
</tr>
</table>
</jacky:form>
</BODY>
</HTML>

jacky_11.jsp :
<%@ page contentType="text/html;charset=big5" session="true" import="com.ucom.dao.sql.*,com.ucom.util.*,java.util.*,com.ucom.jsp.tags.gui.*,com.ucom.jsp.tags.validation.*,com.ucom.jsp.tags.sql.*,com.ucom.jsp.tags.util.*" %>
<%@ taglib uri="/jspwidget" prefix="jacky" %>
<HTML>
<HEAD>
<TITLE></TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=big5" />
</HEAD>
<BODY>
<jacky:form id="jspForm" trace="true">
<%
  String temp01 = (String)session.getAttribute("requiredFieldTextBox1");
  String temp02 = (String)session.getAttribute("requiredFieldDropdownList1");
  out.println("The temp01 is : " + temp01);
  out.println("<br>");
  out.println("The temp02 is : " + temp02);
%>
</jacky:form>
</BODY>
</HTML>

JackyTest.class (servlet 的程式)
package jacky.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import jacky.util.*;

public class JackyTest extends HttpServlet {

/** Initializes the servlet.
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);

}

/** Destroys the servlet.
*/
public void destroy() {

}

/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
response.setContentType("text/html; charset=BIG5");

JackyUtil util = new JackyUtil();
util.processThisRequest(request);
System.out.println("EVERGREEN");
response.sendRedirect("/jacky/jsp/jacky_11.jsp");
}

/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}

/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}

/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}

}

JackyUtil.class (JAVA 的程式)
package jacky.util;

import javax.servlet.*;
import javax.servlet.http.*;

public class JackyUtil{

public void processThisRequest(HttpServletRequest request) {
HttpSession user_session = request.getSession();

if (request.getParameter("requiredFieldTextBox1") != null) {
  user_session.setAttribute("requiredFieldTextBox1", request.getParameter("requiredFieldTextBox1"));
}

if (request.getParameter("requiredFieldDropdownList1") != null) {
  user_session.setAttribute("requiredFieldDropdownList1", request.getParameter("requiredFieldDropdownList1"));
}
}
}

開始測試
1. 在網頁上輸入
http://localhost:8080/jacky/jsp/jacky_10.jsp
這時候就可以試試沒輸入和有輸入的差別(宣告式欄位編審(Declarative Validation))
2. 按下〝送出〞的按鈕後
把結果傳回 servlet (JackyTest.class) (扮演的是 Controller 的角色)
3. 然後 servlet 呼叫專門處理邏輯的程式 (JackyUtil.class)
把傳過來的值寫到 session 中(扮演的是 Model 的角色)
4. 最後呼叫 JSP 來呈現 (jacky_11.jsp)(扮演的是 View 的角色)

個人覺得 JSPWidget 還蠻好用的
可以幫我解決不少 UI 方面控制的問題
有興趣的話
大家可以試試唷~~~

JSPWidget 的相關網址
http://edu.uuu.com.tw/jspwidget/


why edited on 2003-07-11 20:33


我有一点感想和初学者们同享(原创)

话题树型展开
人气 标题 作者 字数 发贴时间
3293 解決JSP程式撰寫不直覺、程式碼與UI混雜的痛: JSPWidget (0.9.4 版新增加 action 的功能) lee5518 7673 2003-07-11 16:00
2808 Re:解決JSP程式撰寫不直覺、程式碼與UI混雜的痛: JSPWidget (0.9.4 版新增加 action 的功能) supertoy 311 2003-07-14 11:49

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923