蜜臀av在线播放,国产精品久久久久久久久图文区,久久国产三级精品http://www.aygfsteel.com/liaojiyong/category/11436.htmlzh-cnWed, 16 May 2007 07:36:47 GMTWed, 16 May 2007 07:36:47 GMT60Struts ActionForm的優化寫法(轉)http://www.aygfsteel.com/liaojiyong/archive/2007/05/16/117833.htmlliaojiyongliaojiyongWed, 16 May 2007 06:25:00 GMThttp://www.aygfsteel.com/liaojiyong/archive/2007/05/16/117833.htmlhttp://www.aygfsteel.com/liaojiyong/comments/117833.htmlhttp://www.aygfsteel.com/liaojiyong/archive/2007/05/16/117833.html#Feedback0http://www.aygfsteel.com/liaojiyong/comments/commentRss/117833.htmlhttp://www.aygfsteel.com/liaojiyong/services/trackbacks/117833.html閱讀全文

liaojiyong 2007-05-16 14:25 發表評論
]]>
tiles-struts(轉)http://www.aygfsteel.com/liaojiyong/archive/2007/03/26/106309.htmlliaojiyongliaojiyongMon, 26 Mar 2007 01:38:00 GMThttp://www.aygfsteel.com/liaojiyong/archive/2007/03/26/106309.htmlhttp://www.aygfsteel.com/liaojiyong/comments/106309.htmlhttp://www.aygfsteel.com/liaojiyong/archive/2007/03/26/106309.html#Feedback0http://www.aygfsteel.com/liaojiyong/comments/commentRss/106309.htmlhttp://www.aygfsteel.com/liaojiyong/services/trackbacks/106309.htmlJavaServer Pages (JSPs)-based implementation, where HTML pages are converted into servlets and JSPs, UI developers identify common HTML and JSP view components, such as header, footer, body, menu, and search. This article presents various solutions to effectively and efficiently organize HTML and JSP view components. I evaluate each solution using specific criteria, such as page number, code repetition, and layout control.

To eXPlore templating and layout solutions, we will use the Tiles framework. The Tiles framework's view components are known as tiles. The framework uses an XML configuration file to organize those tiles. This framework not only enables you to reuse tiles, but also the layouts that organize them.

To eXPlore the more powerful and flexible solutions, we will investigate the synergy between the Tiles and Struts frameworks. Struts is an open source framework for developing Web applications using the popular Model-View-Controller (MVC) or Model 2 architectural pattern. Struts comes with a large set of reusable tags for which the Tiles tag library makes an Excellent enhancement.

Evaluation criteria
I will evaluate each solution based on the criteria below. The criteria are not mutually exclusive. For a specific situation and particular application, you must always balance between the strengths and weaknesses of each solution with respect to these factors.

Page number
A solution should strive to minimize the number of HTML and JSP pages. As the page number increases, the complexity of developing, managing, and maintaining an application increases drastically.

Code repetition
Under most circumstances, repetition is bad. The more repeated HTML and JSP code, the more difficult it is to develop and maintain an application. A simple change can result in a cascade of changes in many different pages with unpredictable consequences. A concrete and practical way of attaining reuse is to avoid code repetition.

Layout control
While code repetition is bad, repetition of layout logic and code can be worse. Spreading the logic and behavior of view component organization over several JSPs can be a recipe for disaster. Attaining reuse of templating and layout logic is a better form of reuse than only reusing view components. Thus, you can achieve a higher level of reuse with effective layout control.

Coupling
Coupling is the degree of interactivity between entities. Software engineers are taught again and again to minimize coupling between unrelated classes, packages, and so on. We can apply the same principle to view components. Even though there are distinct view components from a user perspective, in the JSP implementation, the components might be intricately coupled. A solution should reduce coupling between unrelated view components.

Complexity
Complexity brings increased development and maintenance costs, making a more complex solution less suitable. Complexity grows fast as well, and what might originally look simple and innocuous can quickly turn into a big mess as you add more pieces.

Solutions
I'll evaluate several solutions using a basic example of JSPs with common view components, like header and footer. I'll present these solutions in order of increasing complexity, and then I'll measure in detail each one against the evaluation criteria.

Solution 1: Basic JSP
Consider the following JSP for a.jsp:

<html>

<body>

Header
<p>

a's body...
<p>

Footer
<p>

</body>

</html>

Consider the following JSP for b.jsp:

<html>

<body>

Header
<p>

b's body...
<p>

Footer
<p>

</body>

</html>

In many cases, the developers obtain the code from the UI group and literally convert it into a JSP as necessary. As shown above, each JSP has a duplicate header and footer. Solution 1 is undesirable because changes in common view components, like header and footer, require changes in all relevant pages, as each page is responsible for laying out the view components. This simple solution lacks foresight. With so much HTML and JSP code duplication, we minimize the number of pages but at a heavy maintenance cost. There is strong coupling between the different view components, which, as I eXPlained earlier, is undesirable.

Solution 2: JSP include
Consider the following JSP for a.jsp:

<html>
<body>

<%-- include header --%>
<jsp:include page="/header.jsp" />

a's body...
<p>

<%-- include footer --%>
<jsp:include page="/footer.jsp" />

</body>
</html>

Consider the following JSP for b.jsp:

<html>
<body>

<%-- include header --%>
<jsp:include page="/header.jsp" />

b's body...
<p>

<%-- include footer --%>
<jsp:include page="/footer.jsp" />

</body>
</html>

Note that common view components, like header and footer, are split up using the JSP include mechanism.

Consider this header.jsp:

Header
<p>

Consider this footer.jsp:

Footer
<p>

Solution 2 nicely addresses some of Solution 1's major shortcomings. You only need to change common view components once. Hence, this solution greatly eliminates HTML and JSP code repetition, significantly improving application maintainability. It increases the page number a bit, but drastically reduces the tight coupling between common view components and other pages. On the complexity scale, this solution is simple and readily implemented on many real-world applications. However, it has one major drawback: if you change how and where you organize the view components (i.e., by changing the component layout), then you would need to update every page -- resulting in an eXPensive and prohibitive change. Solution 2 achieves view component reuse, but does not achieve the reuse of layout and templating logic.

Solution 3: Tiles insert
Consider this JSP for a.jsp:

<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %>

<html>
<body>

<%-- include header --%>
<tiles:insert page="/header.jsp" flush="true"/>

a's body...
<p>

<%-- include footer --%>
<tiles:insert page="/footer.jsp" flush="true"/>

</body>
</html>

Consider this JSP for b.jsp:

<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %>

<html>
<body>

<%-- include header --%>
<tiles:insert page="/header.jsp" flush="true"/>

b's body...
<p>

<%-- include footer --%>
<tiles:insert page="/footer.jsp" flush="true"/>

</body>
</html>

Instead of using the JSP include mechanism, Solution 3 uses the Tiles insert mechanism. Using the Tiles insert tag, you include the view components in the appropriate positions. In all other ASPects, the solution mirrors the JSP include solution (Solution 2) exactly, with the same advantages and disadvantages.

Solution 4: Splitting bodies
Consider this a.jsp:

<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %>

<html>
<body>

<%-- include header --%>
<tiles:insert page="/header.jsp" flush="true"/>

<%-- include body --%>
<tiles:insert page="aBody.jsp" flush="true"/>

<%-- include footer --%>
<tiles:insert page="/footer.jsp" flush="true"/>

</body>
</html>

Consider this b.jsp:

<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %>

<html>
<body>

<%-- include header --%>
<tiles:insert page="/header.jsp" flush="true"/>

<%-- include body --%>
<tiles:insert page="bBody.jsp" flush="true"/>

<%-- include footer --%>
<tiles:insert page="/footer.jsp" flush="true"/>

</body>
</html>

Solution 4 differs slightly from the Tiles insert solution. Solution 4 separates the core bodies into their individual pages, like aBody.jsp and bBody.jsp.

Consider the following JSP for aBody.jsp:

a's body...
<p>

Consider the following JSP for bBody.jsp:

b's body...
<p>

Solution 4's advantage: it limits body changes to the respective pages. Also, it lets you reuse the bodies in other places, eliminating the need for repetition and duplication. Thus, the solution further diminishes the coupling between common view components and other application components. Creating and managing each body component introduces an additional complexity level. As with other solutions, each page still does its own layout. Hence, there is no overarching layout policy or scheme.

Solution 5: Templating tiles
Using Tiles's templating feature, you can define the following layout (from the layout.jsp file shown below) as a template. Since this is a layout, you insert placeholders instead of the actual view components using the Tiles insert tag. Thus, for all components, this page defines one reusable layout:

<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %>

<html>
<body>

????<%-- include header --%>
????<tiles:insert attribute="header"/>
????
????<%-- include body --%>
????<tiles:insert attribute="body"/>
????
????<%-- include footer --%>
????<tiles:insert attribute="footer"/>

</body>
</html>

Other content pages, like a.jsp and b.jsp, use the above layout for arranging components. In the actual page, you insert the layout using the Tiles insert tag. Using the Tiles put tag, you can specify the actual view components for all placeholders specified in the layout.

Consider this a.jsp:

<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %>

<tiles:insert page="/layout.jsp" flush="true">
????<tiles:put name="header" value="/header.jsp"/>
????<tiles:put name="body" value="/aBody.jsp"/>
????<tiles:put name="footer" value="/footer.jsp"/>????
</tiles:insert>

Consider this b.jsp:

<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %>

<tiles:insert page="/layout.jsp" flush="true">
????<tiles:put name="header" value="/header.jsp"/>
????<tiles:put name="body" value="/bBody.jsp"/>
????<tiles:put name="footer" value="/footer.jsp"/>????
</tiles:insert>

Solution 5's most significant advantage is that it encapsulates the layout scheme or mechanism, drastically reducing the coupling between common view components and other content bodies. However, it increases complexity by introducing another layout page. Understanding and implementing templating can also be difficult at first.

Solution 6: Struts and Tiles
The above layout page, layout.jsp, contains the HTML and JSP code for organizing the components. The content pages, a.jsp and b.jsp, do not contain any HTML code; they just contain the Tiles tags to insert the necessary components. Wouldn't it be nice to specify all the content pages in one XML configuration file?

Let's name that file tileDefinitions.XML and specify its pages as:

<?XML version="1.0" encoding="ISO-8859-1"?>
<component-definitions>
????<definition name="aDef" path="/layout.jsp">
????????<put name="header" value="/header.jsp"/>
????????<put name="footer" value="/footer.jsp"/>
????????<put name="body" value="/aBody.jsp"/>
????</definition>
????<definition name="bDef" path="/layout.jsp">
????????<put name="header" value="/header.jsp"/>
????????<put name="footer" value="/footer.jsp"/>
????????<put name="body" value="/bBody.jsp"/>
????</definition>
????<definition name="cDef" path="/layout.jsp">
????????<put name="header" value="/header.jsp"/>
????????<put name="footer" value="/footer.jsp"/>
????????<put name="body" value="/cBody.jsp"/>
????</definition>
</component-definitions>

Solution 6 eliminates all the content pages, like a.jsp and b.jsp, by putting their definitions in the XML file. Since a resource like a.jsp no longer exists, how can we request it? More importantly, how can we request the definitions in the tileDefinitions.XML file?

The powerful and synergistic integration of Struts and Tiles comes to the rescue. Besides the regular Struts configuration parameters, we specify the configuration file's location as another parameter in the web.XML file, as shown below. Specifying the definitions-config parameter enables Struts to find and know about the Tiles definitions:

??<!-- Standard Action Servlet Configuration (with debugging) -->
??<servlet>
????<servlet-name>action</servlet-name>
<!--
????<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
-->
????<servlet-class>org.apache.struts.tiles.ActionComponentServlet</servlet-class>
????<init-param>
????????<param-name>definitions-config</param-name>
????????<param-value>/WEB-INF/tileDefinitions.XML</param-value>
????</init-param>
????...
??</servlet>

Now, we define a Struts action, which returns a definition specified in the configuration file upon success. The Struts action DoFirst is a nonoperational action, as shown below:

package com.malani.struts.action;

import org.apache.struts.action.*;
import Javax.servlet.http.*;

public class DoFirst extends Action {

????public ActionForward perform(
????????ActionMapping aMapping,
????????ActionForm aForm,
????????HttpServletRequest aRequest,
????????HttpServletResponse aResponse
????) {
????????return aMapping.findForward("success");
????}

}

You cannot invoke a definition directly from the browser, but you can invoke one from Struts as if it is an actual resource. Define the Struts actions in the struts-config.XML file as shown below:

????<action path="/a"
????????????type="com.malani.struts.action.DoFirst"
????>
????????<forward name="success" path="aDef"/>
????</action>

????<action path="/b"
????????????type="com.malani.struts.action.DoFirst"
????>
????????<forward name="success" path="bDef"/>
????</action>

????<action path="/c"
????????????type="com.malani.struts.action.DoFirst"
????>
????????<forward name="success" path="cDef"/>
????</action>

Now, invoke the Struts action by requesting a.do and b.do actions respectively to return the desired resource.

Solution 6's main advantage is that it consolidates all definitions in an XML configuration file. Eliminating the content pages drastically reduces the total page number. By introducing Struts, we turn up the complexity another notch.

Solution 7: Tiles inheritance
In the definitions configuration file, observe that each page's definition looks similar. Each definition has three components, two of which are fixed as header and footer. A powerful Tiles feature enables inheritance between definitions. Hence, you can define a base definition and let the original definitions inherit from that definition. The original definitions must only supply their unique part. The following shows the XML configuration file with inheritance between definitions:

<?XML version="1.0" encoding="ISO-8859-1"?>
<component-definitions>
????<definition name="baseDef" path="/layout.jsp">
????????<put name="header" value="/header.jsp"/>
????????<put name="footer" value="/footer.jsp"/>
????????<put name="body" value=""/>
????</definition>
????<definition name="aDef" extends="baseDef">
????????<put name="body" value="/aBody.jsp"/>
????</definition>
????<definition name="bDef" extends="baseDef">
????????<put name="body" value="/bBody.jsp"/>
????</definition>
????<definition name="cDef" extends="baseDef">
????????<put name="body" value="/cBody.jsp"/>
????</definition>
</component-definitions>

Elimination of duplicate and redundant information in the configuration file is an advantage of this solution. Overall, the advantages and disadvantages of this solution are identical to the Struts and Tiles solution.

Solution summary
The following table summarizes the different solutions with respect to the evaluation criteria. I encourage you to add other creative solutions as well as other important evaluation criteria, such as extensibility, maintainability, and performance.

Evaluate various solutions per specified criteria

SolutionPage numberCode repetitionLayout controlCouplingComplexity
1: Basic*********
2: JSP include********
3: Tiles insert********
4: Splitting bodies***********
5: Templating tiles**********
6: Struts and Tiles**********
7: Tiles inheritance**********
Scale: High: ***???Medium: **???Low: *

The table shows that each solution's complexity level gradually increases. It also shows that as you increase complexity, you reduce code repetition, increase layout-control flexibility, and diminish coupling between unrelated view components. The page number initially increases as various view components split, but as you define more pages in a definitions configuration file, consolidation will occur.

Divine design
In this article, I evaluated various solutions for organizing view components in HTML and JSPs. I also eXPlored the synergy between the Struts and Tiles frameworks. These strategies and solutions will help you make informed design and architectural decisions regarding your Web applications.

I would sincerely like to thank Max Cooper, Stephen Ditlinger, Dru Jensen, Phillip Lindsay, Roshni Malani, Danny Trieu, and Clare Zhang for reviewing this article.Javaworld/Javaworld/icons/jw-dingbat.gif" width=22>


Printer-friendly versionJavaworld.com/images/jw-print-icon.gif" width=16 align=absMiddle vspace=4 border=0> Javaworld.com/Javaworld/jw-01-2002/jw-0104-tilestrut_p.html">Printer-friendly versionSend this article to a friendJavaworld.com/images/jw-email-icon.gif" width=20 border=0> Javascript:mail2Friend()">Mail this to a friend

About the author
Javascript:openBrWindow()">Prakash Malani has extensive eXPerience in designing and developing object-oriented software using Java and C++. He has been developing software in many application domains, such as e-commerce, retail, medicine, communications, and interactive television. He practices leading technologies, such as object-oriented analysis & design (OOAD), the Unified Modeling Language (UML), XML, Enterprise JavaBeans (EJBs), JavaServer Pages (JSPs), and more. He teaches Java and related technologies at various institutions, including University of California, Irvine and California State Polytechnic University, Pomona.

    Resources
  • Download the complete source code for Solution 1. Basic JSP:
    Javaworld.com/Javaworld/jw-01-2002/tilestrut/1_basic.zip">http://www.Javaworld.com/Javaworld/jw-01-2002/tilestrut/1_basic.zip
  • Download the complete source code for Solution 2. JSP include:
    Javaworld.com/Javaworld/jw-01-2002/tilestrut/2_JSPinclude.zip">http://www.Javaworld.com/Javaworld/jw-01-2002/tilestrut/2_JSPinclude.zip
  • Download the complete source code for Solution 3. Tiles insert:
    Javaworld.com/Javaworld/jw-01-2002/tilestrut/3_tilesInsert.zip">http://www.Javaworld.com/Javaworld/jw-01-2002/tilestrut/3_tilesInsert.zip
  • Download the complete source code for Solution 4. Splitting bodies:
    Javaworld.com/Javaworld/jw-01-2002/tilestrut/4_bodies.zip">http://www.Javaworld.com/Javaworld/jw-01-2002/tilestrut/4_bodies.zip
  • Download the complete source code for Solution 5. Templating tiles:
    Javaworld.com/Javaworld/jw-01-2002/tilestrut/5_templatingTiles.zip">http://www.Javaworld.com/Javaworld/jw-01-2002/tilestrut/5_templatingTiles.zip
  • Download the complete source code for Solution 6. Struts and Tiles:
    Javaworld.com/Javaworld/jw-01-2002/tilestrut/6_struts.zip">http://www.Javaworld.com/Javaworld/jw-01-2002/tilestrut/6_struts.zip
  • Download the complete source code for Solution 7. Tiles inheritance:
    Javaworld.com/Javaworld/jw-01-2002/tilestrut/7_inheritance.zip">http://www.Javaworld.com/Javaworld/jw-01-2002/tilestrut/7_inheritance.zip
  • The Tiles framework:
    http://www.lifl.fr/~dumoulin/tiles/
  • See also "Strut Your Stuff with JSP Tags," Thor Kristmundsson (JavaWorld, December 2000):
    Javaworld.com/Javaworld/jw-12-2000/jw-1201-struts.html">http://www.Javaworld.com/Javaworld/jw-12-2000/jw-1201-struts.html
  • Java Servlet and JavaServer Pages specification:
    Java/communityprocess/final/jsr053/">http://www.jcp.org/aboutJava/communityprocess/final/jsr053/
  • Browse JavaWorld's Servlets Index:
    Javaworld.com/channel_content/jw-servlets-index.shtml">http://www.Javaworld.com/channel_content/jw-servlets-index.shtml
  • Browse JavaWorld's JavaServer Pages Index:
    Javaworld.com/channel_content/jw-jsp-index.shtml">http://www.Javaworld.com/channel_content/jw-jsp-index.shtml
  • Browse JavaWorld's User Interface Design Index:
    Javaworld.com/channel_content/jw-ui-index.shtml">http://www.Javaworld.com/channel_content/jw-ui-index.shtml
  • Subscribe to JavaWorld's free weekly email newsletters:
    Javaworld.com/subscribe">http://www.Javaworld.com/subscribe
  • Speak out in JavaWorld's Java Forum:
    http://forums.devworld.com/webx?13@@.ee6b802
  • You'll find a wealth of IT-related articles from our sister publications at .net/jump?id=1100">IDG.net


liaojiyong 2007-03-26 09:38 發表評論
]]>
Struts+Hibernate中解決漢字編碼問題(轉)http://www.aygfsteel.com/liaojiyong/archive/2007/03/23/105721.htmlliaojiyongliaojiyongFri, 23 Mar 2007 01:42:00 GMThttp://www.aygfsteel.com/liaojiyong/archive/2007/03/23/105721.htmlhttp://www.aygfsteel.com/liaojiyong/comments/105721.htmlhttp://www.aygfsteel.com/liaojiyong/archive/2007/03/23/105721.html#Feedback0http://www.aygfsteel.com/liaojiyong/comments/commentRss/105721.htmlhttp://www.aygfsteel.com/liaojiyong/services/trackbacks/105721.htmlUTF-8格式,至于為什么要采用UTF-8.  
<%@ page language="java" contentType="text/html; charset=utf-8" %>

2. struts框架提供了資源信息文件,它包含了jsp頁面內容的一些文字說明,以及另一些供代碼中使用的信息輸出等(說白了,就是些文字描述定義,可以當成變量看待)。
ApplicationResources.properties(默認的是英文ApplicationResources.properties一份,命名為:ApplicationResources_zh_CN.properties 簡體中文
ApplicationResources_zh_TW.properties 繁體中文
 
然后,執行JDK自帶的命令如下:
native2ascii -encoding gb2312 ApplicationResources_demo.properties ApplicationResources_zh_CN.properties
3. struts中,對于中文參數的傳遞的文字編碼處理,一般加個過濾器類就可以了的。
java源代碼如下:

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.UnavailableException;


public class SetCharacterEncodingFilter implements Filter
{
?protected String encoding = null;
?protected FilterConfig filterConfig = null;
?protected boolean ignore = true;
?
?public void destroy()
?{
??this.encoding = null;
??this.filterConfig = null;
?}

?public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain)
???throws IOException, ServletException
?{
??if (ignore || (request.getCharacterEncoding() == null))
??{
???String encoding = selectEncoding(request);
???if (encoding != null)
????request.setCharacterEncoding(encoding);
??}
??chain.doFilter(request, response);
?}

?public void init(FilterConfig filterConfig) throws ServletException
?{
??this.filterConfig = filterConfig;
??this.encoding = filterConfig.getInitParameter("encoding");
??String value = filterConfig.getInitParameter("ignore");
??if (value == null)
???this.ignore = true;
??else if (value.equalsIgnoreCase("true"))
???this.ignore = true;
??else if (value.equalsIgnoreCase("yes"))
???this.ignore = true;
??else
???this.ignore = false;
?}

?protected String selectEncoding(ServletRequest request)
?{
??return (this.encoding);
?}
}
4.? 配置web.xml文件

? 在<web-app>下加下面的代碼

<filter>
?<filter-name>Set Character Encoding</filter-name>
?<filter-class>com.zxjsoft.util.SetCharacterEncodingFilter</filter-class>
?<init-param>
??<param-name>encoding</param-name>
??<param-value>utf-8</param-value>
?</init-param>
?<init-param>
??<param-name>ignore</param-name>
??<param-value>true</param-value>
?</init-param>
</filter>

<filter-mapping>
?<filter-name>Set Character Encoding</filter-name>
?<servlet-name>action</servlet-name>
</filter-mapping>

5.對于hibernate如說 還得配置hibernate.cfg.xml文件
?在文件里加入
?<property name="hibernate.connection.useUnicode">true</property>
?<property name="hibernate.connection.characterEncoding">UTF-8(或者gb2312)</property>



liaojiyong 2007-03-23 09:42 發表評論
]]>
struts多國語言國際化處理(現以中文英文切換為例) (轉)http://www.aygfsteel.com/liaojiyong/archive/2007/03/23/105717.htmlliaojiyongliaojiyongFri, 23 Mar 2007 01:39:00 GMThttp://www.aygfsteel.com/liaojiyong/archive/2007/03/23/105717.htmlhttp://www.aygfsteel.com/liaojiyong/comments/105717.htmlhttp://www.aygfsteel.com/liaojiyong/archive/2007/03/23/105717.html#Feedback0http://www.aygfsteel.com/liaojiyong/comments/commentRss/105717.htmlhttp://www.aygfsteel.com/liaojiyong/services/trackbacks/105717.html1.引言
? 因為最近有個英國的項目要求中英文切換,又由于我們開發是用struts+hibernate的,所以我們就把原先做兩個版本的構思拋掉,就用struts的國際化處理方案。
2.資料
? 首先,我們要準備兩份配置文件中文和英文的
? 中文:ApplicationResources_zh_CN.properties
? 英文:ApplicationResources_en.properties? (千萬不要用默認的ApplicationResources.properties myeclipse自動生成,可能會不成功,我起先就是因為這個怎么都不行的,呵呵!)
3.準備開始
1)?ApplicationResources_zh_CN.properties的內容
#zh_CN
username=純冰
2)ApplicationResources_en.properties的內容
#en
username=chunkyo
3)一個action
/*
?* Generated by MyEclipse Struts
?* Template path: templates/java/JavaClass.vtl
?*/
package com.lan.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.apache.struts.Globals;
import java.util.Locale;

/**
?* MyEclipse Struts
?* Creation date: 02-07-2007
?*
?* XDoclet definition:
?* @struts.action validate="true"
?*/
public class ChangeLanguageAction extends Action {
?/*
? * Generated Methods
? */

?/**
? * Method execute
? * @param mapping
? * @param form
? * @param request
? * @param response
? * @return ActionForward
? */
?public ActionForward execute(ActionMapping mapping, ActionForm form,
???HttpServletRequest request, HttpServletResponse response) {
??// TODO Auto-generated method stub
??String lan=request.getParameter("lan");
??if(lan.equals("1"))
??{
???request.getSession().setAttribute(Globals.LOCALE_KEY,Locale.CHINA);
??}
??else if(lan.equals("0"))
??{
???request.getSession().setAttribute(Globals.LOCALE_KEY,Locale.ENGLISH);
??}
??else
??{
???request.getSession().setAttribute(Globals.LOCALE_KEY,Locale.CHINA);
??}
??return mapping.findForward("index");
?}
}

4)一個頁面
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
? <head>
??? <html:base />
???
??? <title><bean:message key="username"/></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>
??? <html:link page="/changeLanguage.do?lan=0">English</html:link>
??? <html:link page="/changeLanguage.do?lan=1">Chinese</html:link>
??? <bean:message key="username"/>
? </body>
</html:html>
4)配置文件(struts-config.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>
? <data-sources />
? <form-beans />
? <global-exceptions />
? <global-forwards >
??? <forward name="index" path="/index.jsp" />

? </global-forwards>

? <action-mappings >
??? <action path="/changeLanguage" type="com.lan.struts.action.ChangeLanguageAction" />

? </action-mappings><message-resources parameter="com.lan.struts.ApplicationResources" />
</struts-config>

5)結果
?1。默認
http://localhost:8080/lan/
EnglishChinese 純冰
2。英文
http://localhost:8080/lan/changeLanguage.do?lan=0
EnglishChinese chunkyo
3。中文
http://localhost:8080/lan/changeLanguage.do?lan=1
EnglishChinese 純冰
6)結論
用struts做國際化處理 多國語言處理很方便 呵呵!



liaojiyong 2007-03-23 09:39 發表評論
]]>
Struts2與Struts1的對比 http://www.aygfsteel.com/liaojiyong/archive/2007/01/12/93428.htmlliaojiyongliaojiyongFri, 12 Jan 2007 06:30:00 GMThttp://www.aygfsteel.com/liaojiyong/archive/2007/01/12/93428.htmlhttp://www.aygfsteel.com/liaojiyong/comments/93428.htmlhttp://www.aygfsteel.com/liaojiyong/archive/2007/01/12/93428.html#Feedback0http://www.aygfsteel.com/liaojiyong/comments/commentRss/93428.htmlhttp://www.aygfsteel.com/liaojiyong/services/trackbacks/93428.htmlAction 類:
? Struts1要求Action類繼承一個抽象基類。Struts1的一個普遍問題是使用抽象類編程而不是接口。
? Struts 2 Action類可以實現一個Action接口,也可實現其他接口,使可選和定制的服務成為可能。Struts2提供一個ActionSupport基類去實現 常用的接口。Action接口不是必須的,任何有execute標識的POJO對象都可以用作Struts2的Action對象。
線程模式:
? Struts1 Action是單例模式并且必須是線程安全的,因為僅有Action的一個實例來處理所有的請求。單例策略限制了Struts1 Action能作的事,并且要在開發時特別小心。Action資源必須是線程安全的或同步的。
? Struts2 Action對象為每一個請求產生一個實例,因此沒有線程安全問題。(實際上,servlet容器給每個請求產生許多可丟棄的對象,并且不會導致性能和垃圾回收問題)

Servlet 依賴:
? Struts1 Action 依賴于Servlet API ,因為當一個Action被調用時HttpServletRequest 和 HttpServletResponse 被傳遞給execute方法。
? Struts 2 Action不依賴于容器,允許Action脫離容器單獨被測試。如果需要,Struts2 Action仍然可以訪問初始的request和response。但是,其他的元素減少或者消除了直接訪問HttpServetRequest 和 HttpServletResponse的必要性。

可測性:
? 測試Struts1 Action的一個主要問題是execute方法暴露了servlet API(這使得測試要依賴于容器)。一個第三方擴展--Struts TestCase--提供了一套Struts1的模擬對象(來進行測試)。
? Struts 2 Action可以通過初始化、設置屬性、調用方法來測試,“依賴注入”支持也使測試更容易。

捕獲輸入:
? Struts1 使用ActionForm對象捕獲輸入。所有的ActionForm必須繼承一個基類。因為其他JavaBean不能用作ActionForm,開發者經常創建多余的類捕獲輸入。動態Bean(DynaBeans)可以作為創建傳統ActionForm的選擇,但是,開發者可能是在重新描述(創建)已經存在的JavaBean(仍然會導致有冗余的javabean)。
? Struts 2直接使用Action屬性作為輸入屬性,消除了對第二個輸入對象的需求。輸入屬性可能是有自己(子)屬性的rich對象類型。Action屬性能夠通過web頁面上的taglibs訪問。Struts2也支持ActionForm模式。rich對象類型,包括業務對象,能夠用作輸入/輸出對象。這種ModelDriven 特性簡化了taglib對POJO輸入對象的引用。

表達式語言:
? Struts1 整合了JSTL,因此使用JSTL EL。這種EL有基本對象圖遍歷,但是對集合和索引屬性的支持很弱。
? Struts2可以使用JSTL,但是也支持一個更強大和靈活的表達式語言--"Object Graph Notation Language" (OGNL).

綁定值到頁面(view):
? Struts 1使用標準JSP機制把對象綁定到頁面中來訪問。
? Struts 2 使用 "ValueStack"技術,使taglib能夠訪問值而不需要把你的頁面(view)和對象綁定起來。ValueStack策略允許通過一系列名稱相同但類型不同的屬性重用頁面(view)。
 
類型轉換:
? Struts 1 ActionForm 屬性通常都是String類型。Struts1使用Commons-Beanutils進行類型轉換。每個類一個轉換器,對每一個實例來說是不可配置的。
? Struts2 使用OGNL進行類型轉換。提供基本和常用對象的轉換器。

校驗:
? Struts 1支持在ActionForm的validate方法中手動校驗,或者通過Commons Validator的擴展來校驗。同一個類可以有不同的校驗內容,但不能校驗子對象。
? Struts2支持通過validate方法和XWork校驗框架來進行校驗。XWork校驗框架使用為屬性類類型定義的校驗和內容校驗,來支持chain校驗子屬性

Action執行的控制:
? Struts1支持每一個模塊有單獨的Request Processors(生命周期),但是模塊中的所有Action必須共享相同的生命周期。
? Struts2支持通過攔截器堆棧(Interceptor Stacks)為每一個Action創建不同的生命周期。堆棧能夠根據需要和不同的Action一起使用。



liaojiyong 2007-01-12 14:30 發表評論
]]>
完全解決java開發中的中文問題(側重struts(轉)http://www.aygfsteel.com/liaojiyong/archive/2006/09/01/67079.htmlliaojiyongliaojiyongFri, 01 Sep 2006 05:42:00 GMThttp://www.aygfsteel.com/liaojiyong/archive/2006/09/01/67079.htmlhttp://www.aygfsteel.com/liaojiyong/comments/67079.htmlhttp://www.aygfsteel.com/liaojiyong/archive/2006/09/01/67079.html#Feedback0http://www.aygfsteel.com/liaojiyong/comments/commentRss/67079.htmlhttp://www.aygfsteel.com/liaojiyong/services/trackbacks/67079.html?

?????? 在做java開發的過程中一開始就碰到中文問題,剛接觸到java時用的編譯器是JCreate,這是個非常好用簡單的java編譯器,
但他就在對中文的支持上有很大問題,當時我就在想中國這么大的國家為什么在世界上總是比不過那些小國家,最嚴重的是有日文開發文檔
就是沒有中文的,所以我們在開發java的時候總是會碰到中文問題。下面我就來說一下我在開發中是如何來處理中文的。
????? 首先講中文的字符的編碼,剛開始中文的編碼是GB2312后來發現他的常用字根本不能適應現在的需求,所以就在他的基礎上增加了很多
生僻字,不常用字得到了新的編碼GBK,我們一般來說都用GBK來實現我們的開發,而如果說到國際化就不得不提Unicode (統一碼)顧名思
義是一個將世界上各種文字統一在一起的東東。由美國各大電腦廠商組成的Unicode策進會來推動。目的,推廣一個世界通用的編碼體制,
驚世界上所有常用的文字都涵蓋進去,從而減少個電腦商開發國外市場遇到的問題。
??????? 這些編碼系統也會互相沖突。也就是說,兩種編碼可能使用相同的數字代表兩個不同的字符,或使用不同的數字代表相同的字符。任何
一臺特定的計算機(特別是服務器)都需要支持許多不同的編碼,但是,不論什么時候數據通過不同的編碼或平臺之間,那些數據總會有損壞
的危險。
??? Unicode給每個字符提供了一個唯一的數字,不論是什么平臺,不論是什么程序,不論什么語言。Unicode標準已經被這些工業界的領導
們所采用,例如:Apple, HP, IBM, JustSystem, Microsoft, Oracle, SAP, Sun, Sybase, Unisys和其它許多公司。最新的標準都需要
Unicode,例如XML, Java, ECMAScript (JavaScript), LDAP, CORBA 3.0, WML等等,并且,Unicode是實現ISO/IEC 10646的正規方式。許多
操作系統,所有最新的瀏覽器和許多其他產品都支持它。Unicode標準的出現和支持它工具的存在,是近來全球軟件技術最重要的發展趨勢。
?????? 而Unicode4.0后有了UTF (Unicode/UCS Transformation Format),Unicode推薦使用UTF-8和UTF-16兩種格式其中8和16指的是Bits數而不
是Bytes數。
UTF-16基本就是Unicode雙字節的實現,加上一個應付未來需要的擴充編碼機制(很少用)
UTF-8 是一種不等幅的編碼方式,英數字(Ascii字碼)保持原狀,完全不受影響(因此不需要做轉換),而其他漢字資料須透過程序來轉換,
會[變胖],因為每個字需要額外一個或兩個Bytes來編碼。
?????? 下面我們來看看實戰中我是如何做的:
??? 1.web.xml
?配置過濾器
?

1 <!-- ?Filter?Configuration? -->
2 ? < filter >
3 ?? < filter - name > Set?Character?Encoding </ filter - name >
4 ?? < filter - class > cn.redstoneinfo.commons.web.EncodingFilter </ filter - class > ??
5 ? </ filter >
6

????
??? 2.EncodingFilter.java:

?1 ? // 這里設置的默認的編碼,可以在web.xml中設置,如果不設就用這里的
?2 ? protected ?String?encoding? = ? " UTF-8 " ;
?3
?4 ? protected ? boolean ?ignore? = ? true ;
?5
?6 ? /* ?
?7 ??*?@see?javax.servlet.Filter#init(javax.servlet.FilterConfig)
?8 ?? */

?9 ? public ? void ?init(FilterConfig?filterConfig)? throws ?ServletException? {
10 ??String?paramValue? = ?filterConfig.getInitParameter( " encoding " );
11 ?? if ?(paramValue? != ? null )? {
12 ??? this .encoding? = ?paramValue;
13 ??}

14 ??String?value? = ?filterConfig.getInitParameter( " ignore " );
15 ?? if ?(value? == ? null )
16 ??? this .ignore? = ? true ;
17 ?? else ? if ?(value.equalsIgnoreCase( " true " ))
18 ??? this .ignore? = ? true ;
19 ?? else ? if ?(value.equalsIgnoreCase( " yes " ))
20 ??? this .ignore? = ? true ;
21 ?? else
22 ??? this .ignore? = ? false ;
23 ?}

24

?

??? 3.Struts-config.xml:
?設置資源文件的路徑
?

1 < message - resources
2 ??parameter = " cn.redstoneinfo.oss.security.web.security-resource " ?key = " SECURITY_RES " ? null = " false " />
3 ??
4 ? < message - resources
5 ??parameter = " cn.redstoneinfo.oss.web.ApplicationResources " ? null = " false " />
6

??? 4.role-list.jsp
?頁面編碼和調用資源
?

?1 <% @?page?contentType = " text/html;?charset=UTF-8 " ?language = " java " %> ?
?2 ????????
?3 ? < oss:panel?label = " security.role.list.title " ?bundle = " SECURITY_RES " >
?4 ? < table?width = " 100% " >
?5 ? < tr? class = " tr1 " ?align = " center " ?height = " 25 " >
?6 ????? < td?width = " 5% " >
?7 ????? </ td >
?8 ????? < td? > ??
?9 ?? < bean:message?key = " app.common.name " ? />
10 ????? </ td >
11 ????? < td? >
12 ????? < bean:message?key = " app.common.activeDate " ? />
13 ????? </ td >
14 ????? < td >
15 ????? < bean:message?key = " app.common.inactiveDate " ? />
16 ????? </ td? >
17 ????? < td >
18 ????? < bean:message?key = " app.common.status " ? />
19 ????? </ td > ???
20 ????
21 ? </ tr >
22 ????????
23 ???????? < input?type = " button " ?value = " ???<bean:message?key= " app.common. new " /> " ? class = " buttonnew " ?onclick = " javascript:document.forms[0].act.value='add';?goUrlFormTarget('role.do',?'roleForm',?'roleTreeFrame'); " > ????
24


??? 5.ApplicationResources.properties
?

1 app.common.name? = ?名字
2 ?app.common.ip? = ?IP
3 ?app.common.port? = ?端口
4 ?app.common.status? = ?狀態?
5

??? 6.build.xml
?編譯資源文件生成Unicode

1 ? < native2ascii?src = " ${oss.src.dir} " ?encoding = " UTF-8 " ?dest = " ${target.dir}/WEB-INF/classes " >
2 ??? < include?name = " **/*.properties " ? />
3 ?? </ native2ascii > ??
4

??? 7.轉換后的ApplicationResources.properties
?

1 app.common.name? = ?\u540d\u5b57
2 ?app.common.ip? = ?IP
3 ?app.common.port? = ?\u7aef\u53e3
4 ?app.common.status? = ?\u72b6\u6001
5

??? 8.server.xml
?tomcat5.0以后post跟get提交方法不了不同的編碼處理,應該用下面的方式來處理
????

1 < Connector
2 ??port = " 8080 " ???????????????maxHttpHeaderSize = " 8192 "
3 ???????????????maxThreads = " 150 " ?minSpareThreads = " 25 " ?maxSpareThreads = " 75 "
4 ???????????????enableLookups = " false " ?redirectPort = " 8443 " ?acceptCount = " 100 "
5 ???????????????connectionTimeout = " 20000 " ?disableUploadTimeout = " true " ??URIEncoding = " UTF-8 " />
6

??? 9.界面
11.gif
??? 以上操作基本實現徹底消滅中文亂碼問題,當然還有其他更好的辦法和應用,我們這里只講struts的簡單應用中如何避免相應問題的處理辦法。



liaojiyong 2006-09-01 13:42 發表評論
]]>
MyEclipse+struts+Hibernate配置開發(轉)http://www.aygfsteel.com/liaojiyong/archive/2006/06/05/50452.htmlliaojiyongliaojiyongMon, 05 Jun 2006 04:38:00 GMThttp://www.aygfsteel.com/liaojiyong/archive/2006/06/05/50452.htmlhttp://www.aygfsteel.com/liaojiyong/comments/50452.htmlhttp://www.aygfsteel.com/liaojiyong/archive/2006/06/05/50452.html#Feedback0http://www.aygfsteel.com/liaojiyong/comments/commentRss/50452.htmlhttp://www.aygfsteel.com/liaojiyong/services/trackbacks/50452.html

  建議:

  如果你還不清楚struts和hibernate的一些基本原理,希望能先去了解一下這方面的相關內容。

  推薦:

  Hibernate中文手冊》作者認為要學Hibernate看這個就足夠了,里面幾乎包括了所有的細節,不過可能不太適合快速入門。

  地址:http://www.hibernate.org/hib_docs/v3/reference/zh- cn/html_single/

  關于struts的資料就很多了,這里推薦一個可以下載一些入門教程的網站。

  地址:http://www.wnetw.com/jclub/index.jsp

  強烈建議入門的朋友先了解一下基本的原理!否則本文可能對你沒有任何幫助。

  相關工具下載:(注意版本)

  mysql5.0 http://www.mysql.org
  eclipse 3.1.1 http://www.eclipse.org
  myeclipse4.0.3 http://www.myeclipseide.com
  tomcat5.5

  安裝:

  關于tomcat和mysql的安裝就不多說了,需要注意的是最好保證你的 jdk是1.5的版本,并配置好你的環境變量,不然可能會遇到一些問題。

  把eclipse解開,再去安裝剛下載的myeclipse,在安裝的時候需要把路徑指定到剛才解開的eclipse上,由于myeclipse是個收費軟件,所以需要注冊。不過一般按照Chinese的習慣,去google一個注冊碼就可以了:}

  開發環境部署:

  好了,現在保證你的mysql和tomcat服務能夠正常啟動,myeclipse能夠正常打開(如果不能,可以去找一下相關的說明或者給作者留言)。下面我們就要開始真正的開始部署一個傳說中的tomcat+struts+hibernate+mysql結構的工程了!(faint!前言就寫的我好累)

  首先,在myeclipse里新建一個工程。在左邊的Package Exporler面版里點右鍵選擇new->project…

  在跳出菜單里選擇MyEclipse->J2EE Projects->Web Project。

  點擊next后進入如下畫面:


  工程名為:test

  結束后點擊Finish。

  好了,如果成功的話你就會在 Package Exporler里看到一個新的test工程!現在我們先配置一下數據庫方面的東西。首先在你的mysql 里建立一個數據庫webases,再在里面新建一個表admin,里面三個字段分別為id,name,password其中id為自動取值的主鍵(mysql具體的操作可以自己找資料,不是本文涉及范圍)。

  再回到myeclipse ,選中window->Open Perspective->Other…

  可以看到現在跳出一個名為Select Perspective的菜單,在里面選中MyEclipse Databases Exporler,可以看到現在到了下面的頁面。

  按以上圖示輸入相關字段后點擊Finish便建立了一個數據庫連接,在新出現的JDBC for Mysql上點右鍵,選擇Open connection…,確認用戶名和密碼正確后點OK,如果一切順利的話你會看到下面的畫面:

  這說明你已經和數據庫建立了正確的連接。現在我們再回到window->Open Perspective- >Other…里的MyEclipse,也就是我們剛進來的時候看到的畫面。

  右鍵點擊你剛建立的工程 test并選擇MyEclipse->Add struts Capabilities…在跳出的菜單里按照如下輸入并確定:

  好了,現在你已經為你的工程增加了struts,接下來和上面一樣在右鍵工程后選擇MyEclipse- >Add Hibernate Capabilities…一路確定下來為你的工程添加Hibernate。(為方便起見我們在選擇路徑時把HibernateSessionFactory.java放在了src/com下面,其實最好建立個單獨的目錄如 src/com/hibernate)

  為了更好的演示我們不建立通常的登陸頁面而是建立個注冊頁面。選擇 src目錄下的hibernate.cfg.xml文件。照如下填寫并保存。這樣hibernate就為你建立了數據庫的連接池。

  下面我們再選擇WebRoot/WEB-INF/struts-config.xml文件,在畫面中點擊右鍵選擇new- >Form, Action and JSP。如下填寫

  再選擇JSP選項,如下

  最后選擇Finish。

  再新建一個一個success.jsp的頁面,

  在剛才struts- config.xml文件里右鍵選擇addAdmin選擇Properties,在菜單里選擇Forwords,再點add,如下圖填寫

  最后你的struts-config.xml就是下面這個樣子:


  下面我們轉到hibernate。換到剛才我們建立數據庫的頁面,選擇你的admin的表點右鍵選擇Create Hibernate Mapping。選擇好打包路徑后選擇Finish。如圖:

  在你剛才選擇的路徑下(我為方便是src/com/yourcompanyname/)下新建立的文件 AdminDAOFactory.java文件并輸入以下內容:

package com.yourcompanyname;

import java.util.Iterator;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.hibernate.SessionFactory;

public class AdminDAOFactory {
?Session session;
?Transaction tx;
?public void add(Admin admin) throws HibernateException {
??/**
?? * Creation Date: 11-17-2005
?? * TODO Add a new admin user.
?? * @param An object of Admin
?? * @return void
?? * @author Coder Guo
?? */
??try {
???session = SessionFactory.currentSession();
???tx = session.beginTransaction();
???//Add a new admin
???session.save(admin);
???tx.commit ();
??}catch(HibernateException e){
???throw e;
??}finally{
???if (tx!=null) {
????tx.rollback();
???}
???SessionFactory.closeSession();
??}
?}
}

?再打開com.yourcompany.struts.action下的AddAdminAction.java添加(其中如果有錯誤選中好按ctrl+shift+o自動添加包)

public class AddAdminAction extends Action {

?// --------------------------------------------------------- Instance Variables

?// --------------------------------------------------------- Methods

?/**
? * Method execute
? * @param mapping
? * @param form
? * @param request
? * @param response
? * @return ActionForward
? * @author Coder Guo
? */
?public ActionForward execute(
??ActionMapping mapping,
??ActionForm form,
??HttpServletRequest request,
??HttpServletResponse response) {
??AddAdminForm addAdminForm = (AddAdminForm) form;
??
??// TODO Add a new admin
??Admin admin = new Admin();
??admin.setName(addAdminForm.getName ());
??admin.setPassword(addAdminForm.getPassword ());
??AdminDAOFactory adminDAO = new AdminDAOFactory ();
??adminDAO.add(admin);
??
??return mapping.findForward("success");
?}

}

?再打開com.yourcompanyname.struts.form下的AddAdminForm.java,修改(如果有錯誤按照上面說的方法導入包)
?public ActionErrors validate(
??ActionMapping mapping,
??HttpServletRequest request) {

??// TODO Auto-generated method stub
??ActionErrors errors = new ActionErrors();
??
??Session session = SessionFactory.currentSession();
??Transaction tx = session.beginTransaction ();
??Query query = session.createQuery("select admin from Admin as admin where admin.name = '" + this.name + "'");
??Iterator it = query.iterate ();
??if (it.hasNext()){
???errors.add ("addAdmin.err.name",new ActionMessage("form.addAdmin.err.name"));
??}
??tx.commit();
??SessionFactory.closeSession ();
??return errors;
?}

?public void reset(ActionMapping mapping, HttpServletRequest request) {

??// TODO Auto-generated method stub
??this.name=null;
??this.password=null;
?}

  再打開com\yourcompanyname\struts下的ApplicationResource.properties在這里面添加錯誤信息:

Form.addAdmin.err.name=err

  最后,(汗,好累啊-_-!)打開addAdmin.jsp修改成如下:

<%@ page contentType="text/html; charset=utf-8"%>
<%@ page language="java"%>
<%@ taglib uri="<%@ taglib uri="

<script language = "javascript">
<!--
function
check(){
?if (loginForm.userName.value == "" || loginForm.password.value == ""){
??alert("請輸入完整的信息!");
??loginForm.userName.focus();
??return false;
?}
}
//-->
</script>
?
?<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html>
? <head>
??? <html:base />
???
??? <title>login.jsp</title>
??? <link href="css/webcss.css" rel="stylesheet" type="text/css">
???
??? <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">
? </head>
?

? <body>
? <center>
? ?<p>&nbsp;</p>
? ?<p>&nbsp;</p>
? ?<table width="300" border="0" cellpadding="0" cellspacing="0">
? ?<html:form action="/addAdmin" focus="name" method="GET">
??? ?<tr align="center" valign="middle">
????? ?<td colspan="2" class="typt_normal">新增管理員</td>
??? ?</tr>
??? ?<tr>
????? ?<td width="100" align="center" valign="middle" class="typt_normal">名稱: </td>
????? ?<td width="200" align="left"><html:text property="name" styleClass="text_s"/><html:errors property="addAdmin.err.name"/></td>
??? ?</tr>
??? ?<tr>
????? ?<td width="100" align="center" valign="middle" class="typt_normal">密碼: </td>
????? ?<td width="200" align="left"><html:password property="password" styleClass="text_s"/></td>
??? ?</tr>
??? ?<tr>?
????? ?<td colspan="2" align="center" valign="middle"><html:submit value="提交" onclick="return check ();"/><html:reset value="重置"></html:reset></td>
????? ?</tr>
?</html:form>
? ?</table>
?</center>
?</body>

</html:html>

  其中可以看到如何在struts的標簽中使用javascript的方法。

  配置好myeclipse于tomcat的連接。在window->Preferences做如下設定:

  在項目文件點右鍵->“myeclipse”->“Add and remove project deployment”,如下圖:

  好了,我們的配置工作基本結束了,在myeclipse上開啟tomcat服務

  現在打開瀏覽器,輸入

  http://127.0.0.1:8080/test/addAdmin.jsp就可以看到你的jsp頁面了!



liaojiyong 2006-06-05 12:38 發表評論
]]>
在struts框架下實現文件的上傳 (轉)http://www.aygfsteel.com/liaojiyong/archive/2006/05/23/47693.htmlliaojiyongliaojiyongTue, 23 May 2006 10:03:00 GMThttp://www.aygfsteel.com/liaojiyong/archive/2006/05/23/47693.htmlhttp://www.aygfsteel.com/liaojiyong/comments/47693.htmlhttp://www.aygfsteel.com/liaojiyong/archive/2006/05/23/47693.html#Feedback0http://www.aygfsteel.com/liaojiyong/comments/commentRss/47693.htmlhttp://www.aygfsteel.com/liaojiyong/services/trackbacks/47693.html

由于jspsmartupload上傳文件,當前端頁面沒有file控件時,后端用jspsmartupload控件upload時將會走入一個死循環。現在采用struts自己提供的功能實現文件的上傳。
1、前端頁面upload.jsp
<html:form action="/filesave.do" styleId="formItem" method="post"? enctype="multipart/form-data">
<INPUT? class='input-file' type="file" name="theFile">
</html:form>
2、struts-config.xml文件配置
<form-bean name="UploadForm" type="yhp.test.web.UploadForm" />
<action input="/upload.jsp" name="UploadForm"?? path="/filesave" scope="request" type="yhp.test.web.UploadAction" validate="false">
????? <forward name="success" path="/success.jsp" />
????? <forward name="error" path="/error.jsp" />
??? </action>
3、UploadForm.java文件
package yhp.test.web;

import org.apache.struts.upload.FormFile;
import org.apache.struts.action.ActionForm;

public class UploadForm extends ActionForm {
? protected String theText;?? //使用protected?,其它需要上傳的form可以繼承此form
? protected boolean writeFile = true;
? protected FormFile theFile;
? protected String filePath;

? public String getTheText() {
??? return theText;
? }
? public void setTheText(String theText) {
??? this.theText = theText;
? }
? public FormFile getTheFile() {
??? return theFile;
? }
? public void setTheFile(FormFile theFile) {
??? this.theFile = theFile;
? }
? public void setWriteFile(boolean writeFile) {
??? this.writeFile = writeFile;
? }
? public boolean getWriteFile() {
??? return writeFile;
? }
? public void setFilePath(String filePath) {
??? this.filePath = filePath;
? }
? public String getFilePath() {
??? return filePath;
? }
? public void reset() {
??? writeFile = false;
? }
}

4、UploadAction.java文件
package yhp.test.web;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;


public class UploadAction extends Action {
? public ActionForward execute(ActionMapping actionMapping,
?????????????????????????????? ActionForm actionForm,
?????????????????????????????? HttpServletRequest httpServletRequest,
?????????????????????????????? HttpServletResponse httpServletResponse) throws
????? Exception {
??? if (actionForm instanceof UploadForm) {
????? // 獲取上傳的目錄
????? String path = getServlet().getServletContext().getInitParameter("UPLOADPATH");
????? File f = new File(path);
????? if (!f.isDirectory()) {
??????? f.mkdir();
????? }

????? UploadForm theForm = (UploadForm) actionForm;
????? String text = theForm.getTheText();
????? FormFile file = theForm.getTheFile();
????? String fileName = file.getFileName();
????? String contentType = file.getContentType();
????? boolean writeFile = theForm.getWriteFile();
????? String desFileName=path fileName ;
????? String size = (file.getFileSize() + " bytes");
????? try {
????????? InputStream stream = file.getInputStream();
?? OutputStream bos = new FileOutputStream(strFileName);?????????
????????? byte[] buffer = new byte[file.getFileSize()];
????????? stream.read(buffer);
????????? bos.write(buffer);
?? stream.close();?
????????? bos.close();
????????? return actionMapping.findForward("success");
????? }
????? catch (FileNotFoundException fnfe) {
????????? return actionMapping.findForward("success");
????? }
????? catch (IOException ioe) {
????????? return actionMapping.findForward("success");
????? }
????? catch(Exception eo){
????????? return actionMapping.findForward("error");
????? }?
??? }else{
????? return actionMapping.findForward("error");
??? }
? }
}



liaojiyong 2006-05-23 18:03 發表評論
]]>
主站蜘蛛池模板: 房产| 怀柔区| 景东| 岱山县| 潜山县| 莱州市| 黄石市| 洛川县| 呼玛县| 临泉县| 视频| 甘南县| 镇平县| 渭南市| 观塘区| 丰台区| 扬州市| 友谊县| 行唐县| 绥化市| 辽中县| 寿宁县| 临城县| 巴楚县| 株洲县| 亳州市| 寿阳县| 古浪县| 湘乡市| 大石桥市| 扎兰屯市| 韩城市| 塔河县| 华蓥市| 乐清市| 雅安市| 宁晋县| 通榆县| 山丹县| 宽城| 印江|