天下之事,必先处之难,而后易之。

Struts1+Hibernate+Spring开发

一、配置及方法调用

1、搭建框架:dao、 entity 、service、web(action、form)。

2、添加支持jar包。

3、配置文件的修改:

  (1)、在web.xml中添加如下代码:

 <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
   </context-param> 

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

(2)、view 设计Action,添加相应参数(注意:继承自DispatchAction)

(3)、在applicationContext.xml文件下编写如下代码:示例

<!-- service -->
 <bean id="caclService" class="com.accp.service.CalcService"></bean>
 
 <!-- action -->
 <bean name="/calc" class="com.accp.web.action.CalcAction">
  <property name="caclService" ref="caclService"></property>
 </bean>

(4)、WEB-INF下struts-config.xml中代码如下所示:

<struts-config>
  <data-sources />
  <form-beans >
    <form-bean name="calcForm" type="org.apache.struts.action.DynaActionForm">
      <form-property name="name" type="java.lang.Double" />
      <form-property name="password" type="java.lang.Double" />
    </form-bean>

  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      attribute="calcForm"
      input="/calc.jsp"
      name="calcForm"
      parameter="method"
      path="/calc"
      scope="request"
      type="org.springframework.web.struts.DelegatingActionProxy">
      <set-property property="cancellable" value="true" />
      <forward
        name="result"
        path="/calc.jsp"
        contextRelative="true" />
    </action>

  </action-mappings>

  <message-resources parameter="ApplicationResources" />
</struts-config>

(5)jsp页面上:

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
 
<html>
 <head>
  <title>JSP for DynaActionForm form</title>
 </head>
 <body bgcolor="#fffeee">
  <form action="calc.do?method=login" method="post">
  <table align="center" width="80%">
  <tr>
      <td colspan="2">
       <jsp:include flush="true" page="top.jsp"></jsp:include>
      </td>
  </tr>
  <tr>
   <td align="right">
                用户名 :
   </td>
   <td align="left">
         <input type="text" name="name" value="${name}"/><font color="red"><html:errors property="name"/></font>
   </td>
  </tr>
  <tr>
   <td align="right">
                密&nbsp;&nbsp;码 :
   </td>
   <td align="left">
          <input type="password" name="password" value="${password}"/><font color="red"><html:errors property="password"/></font>
   </td>
  </tr>
  <tr>
  <td colspan="2" align="center">
        <input type="submit" value="登录"/>&nbsp;<input type="reset" value="取消"/>
       </td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2">
     <jsp:include flush="true" page="bottom.jsp"></jsp:include>
    </td>
  </tr>
     </table>
  </form>
 </body>
</html>

(6)Action方法中示例代码:

public class UserAction extends DispatchAction {
 
    private UserService userService=new UserService();
   
 
 public void setUserService(UserService userService) {
  this.userService = userService;
 }


 public ActionForward login(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  ActionMessages msg=new ActionMessages();
  DynaActionForm userForm = (DynaActionForm) form;
  System.out.println("---------user login--------");
     String name=(String) userForm.get("name");
     request.setAttribute("name", name);
     if(null==name||name.trim().length()==0){
      msg.add("name", new ActionMessage("用户名不能为空!", false));
      super.saveErrors(request, msg);
      return new ActionForward("/user.jsp");
     }
     String password=(String) userForm.get("password");
     request.setAttribute("password", password);
     if(null==password||password.trim().length()==0){
      msg.add("password", new ActionMessage("密码不能为空!", false));
      super.saveErrors(request, msg);
      return new ActionForward("/user.jsp");
     }
     Userinfo user=this.userService.getUser(name,password);
     if(null!=user){
      return mapping.findForward("index");
     }else{
      
     }
  return mapping.findForward("login");

 }
 
}

(7)、方法调用如:calc.do?method=login

二、学习过程中遇到的错误

(1)错误:在某action里使用request.setAttribute()方法将一个List类型对象放在request中,然后forward到一个jsp文件,该文件的主要内容是使用<logic:iterate>标签将这个List对象中的条目列表显示。
与它同样逻辑但位于另一模块(teacher)中的代码执行正常。但这个模块(xxgl)中的代码,本来很简单的逻辑,却总是提示:
org.apache.jasper.JasperException: Cannot find bean t in any scope
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:254)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:432)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:356)
at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)
at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:455)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:279)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2417)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:193)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:781)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:549)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:589)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:666)
at java.lang.Thread.run(Unknown Source)

 (2)解决方案:

1、检查是否引入Struts1标签库

<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>

2、如代码:<div id="note_content">
       <logic:iterate id="note" name="notes" scope="request">
            ${note.content}
       </logic:iterate>
     </div>

将具体访问的scope注明为scope=“request”;

posted @ 2011-08-26 12:51  boonya  阅读(509)  评论(0编辑  收藏  举报
我有佳人隔窗而居,今有伊人明月之畔。
轻歌柔情冰壶之浣,涓涓清流梦入云端。
美人如娇温雅悠婉,目遇赏阅适而自欣。
百草层叠疏而有致,此情此思怀彼佳人。
念所思之唯心叩之,踽踽彳亍寤寐思之。
行云如风逝而复归,佳人一去莫知可回?
深闺冷瘦独自徘徊,处处明灯影还如只。
推窗见月疑是归人,阑珊灯火托手思忖。
庐居闲客而好品茗,斟茶徐徐漫漫生烟。

我有佳人在水之畔,瓮载渔舟浣纱归还。
明月相照月色还低,浅近芦苇深深如钿。
庐山秋月如美人衣,画堂春阁香气靡靡。
秋意幽笃残粉摇曳,轻轻如诉画中蝴蝶。
泾水潺潺取尔浇园,暮色黄昏如沐佳人。
青丝撩弄长裙翩翩,彩蝶飞舞执子手腕。
香带丝缕缓缓在肩,柔美体肤寸寸爱怜。
如水之殇美玉成欢,我有佳人清新如兰。
伊人在水我在一边,远远相望不可亵玩。