五、jsp部分
JSP在应用中作为视图层,在它里面,你看不到传统的<% %>包含的控制语句,取而代之的是Struts的标签控制!
首先,我们需要在guestbook.jpx的应用中建立web application应用,命名为guestbook,点开它下面的Deployment descriptors,我们会看到web.xml和jboss.xml两个文件,它们都是建立web application后自动产生的现在我们双击jboss.xml文件来编辑它,这里我们主要是能让它支持中文!
weblogic.xml源代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE weblogic-web-app PUBLIC "-//BEA Systems, 
Inc.//DTD Web Application 6.1//EN"
"http://www.bea.com/servers/wls610/dtd/weblogic-web-jar.dtd">
<weblogic-web-app>
 <jsp-descriptor>
   <jsp-param>
     <param-name>encoding</param-name>
     <param-value>GBK</param-value>
   </jsp-param>
 </jsp-descriptor>
</weblogic-web-app> 接着我们需要编辑web.xml文件,主要是配置默认的首页文件,Servlets和Tag Libraries。你可以在WebApp DD Editor项目中配置,也可以直接在Source面板中直接编写源代码
web.xml源代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, 
Inc.//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
 <servlet>
   <servlet-name>action</servlet-name>
   <servlet-class>org.apache.struts.action.ActionServlet
</servlet-class>
   <init-param>
     <param-name>config</param-name>
     <param-value>/WEB-INF/struts-config.xml</param-value>
   </init-param>
   <init-param>
     <param-name>debug</param-name>
     <param-value>2</param-value>
   </init-param>
   <init-param>
     <param-name>detail</param-name>
     <param-value>2</param-value>
   </init-param>
   <init-param>
     <param-name>port</param-name>
     <param-value>1099</param-value>
   </init-param>
   <load-on-startup>2</load-on-startup>
 </servlet>
 <servlet-mapping>
   <servlet-name>action</servlet-name>
   <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <taglib>
   <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
 </taglib>
 <taglib>
   <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
 </taglib>
 <taglib>
   <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
 </taglib>
</web-app> 

解决方案 »

  1.   

    在完成以上两个文件的编写后,我们把struts1.1中的所有控制标签文件(除*.jar外的所有文件)拷贝到留言本的应用目录WEB-INF目录中(当你在JBuilder的项目应用中建立web application时,它就会自己帮你建立WEB-INF),如果你以目录形式来发布此次应用,你必须手动在WEB-INF目录下建立一个lib目录,将所有需要的LIB资源文件拷进去,当然如果你在JBuilder中编译此web application它除了会自动将WEB应用打包为guestbook.war文件外,还会自动在WEB-INF中建立LIB文件夹,并将相关的LIB资源文件拷进去!
    接着我们需要建立struts-config.xml文件,它是配置struts控制的重要文件,编写Struts-config.xml的利器非Struts Console不可,下边是详细的图解:
    首先我们新建一个struts config文件,我们选用Struts1.1 Config File,点OK点击form Beans新建一个lyform,NAME:lyform,Type:guestbook.maxuan.action.Lyform然后我们在Action Mappings分别加入lyList、saveLy、editLy三个Action! lyList Action主要是列出所有的留言信息,在path填入/lyList,Type对应它在JAVABEAN中的文件guestbook.maxuan.action.LyListAction(写出完整的文件信息位置),在Forwards加入success,指向正确的JSP文件,这里命名为indexCnt.jsp,我们追溯到/lyList对应的LyListAction.java文件,在最后我们看到的"return (mapping.findForward("success"));"即与此处的success命名对应,也就是成功后即指向success, 
    而在Struts-config.xml释诠了success指向的实际JSP文件是indexCnt.jsp文件!按以上方式我们继续编辑saveLy、editLy,图示如下: 
    saveLy、editLy相对应的是form Beans,所以我们必须在它们的form Bean中指定 
    在完成后,我们再看form Beans中的lyform的Referencing Actions就会看到path中已经有了/saveLy和/editLy最后,我们需要编辑信息资源项目,在Message Resources下新加入New Message Resource,parameter为ApplicationResources,Messages Key为org.apache.struts.action.MESSAGE!由于在此篇教程中并没有涉及到国际化应用,应此我们并没有编写相应的资源文件!
    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>
    <form-beans>
    <form-bean name="lyform" type="guestbook.maxuan.action.Lyform" />
    </form-beans>
    <action-mappings>
    <action path="/lyList" 
    type="guestbook.maxuan.action.LyListAction">
    <forward name="success" path="/indexCnt.jsp" redirect="false" />
    </action>
    <action path="/saveLy" type="guestbook.maxuan.action.LySaveAction" 
    name="lyform" input="/ly.jsp" 
    scope="request">
    <forward name="success" path="/index.jsp" redirect="true" />
    </action>
    <action path="/editLy" type="guestbook.maxuan.action.LyEditAction"
    name="lyform" scope="request">
    <forward name="success" path="/ly.jsp" redirect="false" />
    </action>
    </action-mappings>
    <message-resources parameter="ApplicationResources" 
    key="org.apache.struts.action.MESSAGE" />
    </struts-config> 在完成了配置后,我们开始写JSP页面,首先我们定义template,它是struts的模板,所有相同的部份都可以使用template!
    template.jsp的源代码
    <%@ taglib uri='/WEB-INF/struts-template.tld' prefix='template' %>
    <html>.
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">
    <title>留言本教程范例</title>
    </head>
    <body bgcolor="#ffffff" leftmargin="0" topmargin="0" 
    marginwidth="0" marginheight="0">
    <template:get name='header'/>
    <template:get name='content'/>
    <template:get name='footer'/>
    </body>
    </html> 接着完成首页index.jsp文件,它的模板使用template.jsp,定义了页首(header.htm),页尾(footer.htm),及内容显示(由/lyList.do映射到indexCnt.jsp)
    index.jsp的源代码
    <%@ taglib uri='/WEB-INF/struts-template.tld' prefix='template' %>
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <template:insert template='/template.jsp'>
     <template:put name='header' content='/header.htm'/>
     <template:put name='content' content='/lyList.do'/>
     <template:put name='footer' content='/footer.htm'/>
    </template:insert> 页首header.htm的代码(由于只是教程,我并没有进行页面美化工作,大家可以根据自己的喜爱自定义)
    <table width="90%" height="50" border="0" align="center" 
    cellpadding="0" cellspacing="0">
     <tr>
       <td align="center"><strong>留 言 本</strong></td>
     </tr>
    </table> 页尾footer.htm的源代码
    <table width="90%" border="0" align="center" cellpadding="0" 
    cellspacing="0">
     <tr>
       <td align="center">留言本教程范例 2003-9-21</td>
     </tr>
    </table>