最近开始学struts。先是抱着书看了几天,然后就动手想写个简单的登录程序。但是在写的过程中发现了页面上提交的请求可以被转发到自己写的action,但是action中的方法execute不执行,怎么都调不好。然后写了一个超级简单的程序,只有提交的操作,还是不行,郁闷。第二天跟别人一起找问题,最后终于解决了。
先说一下环境吧: MyEclipse5.5(机子太烂,版本高了跑不动),JAVASDK1.6,struts1.3.10。
项目的结构: page1.jsp,page2.jsp,PageAction.java。 page1提交请求到PageAction.do,PageAction在控制台上打印"success",返回一个"success"。跳转到page2.jsp。
原始代码:
page1.jsp
===========================================
<%@ page contentType="text/html; charset=UTF-8" %>
<form method="POST" action="PageAction.do">
<input type="submit" value="submit" />
</form>
===========================================
page2.jsp
===========================================
<%@ page contentType="text/html; charset=UTF-8" %>
<%
out.println("This is page2.jsp");
%>
===========================================
PageAction.java
===========================================
package com.action;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class PageAction extends Action {
public ActionForward execute(ActionMapping mapping,ActionForm form, ServletRequest request,
ServletResponse response) throws Exception {
System.out.println("success");
return mapping.findForward("success");
}
}
============================================
web.xml
============================================
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
=============================================
struts-config.xml
=============================================
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<action-mappings>
<action path="/PageAction" type="com.action.PageAction" >
<forward name="success" path="/page2.jsp" />
</action>
</action-mappings>
</struts-config>
==============================================
最开始的时候总是点击page1上的按钮后显示一个空白页面,后台也不报错,超郁闷。怎么改都不行。后来看
Action 的源码发现 Action 把 execute 方法重载了。分别是:
=======================================================
public ActionForward execute(ActionMapping mapping, ActionForm form,
ServletRequest request, ServletResponse response)
throws Exception {
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
}
=======================================================
然后跟踪程序的执行流程。 发现流程里有这样一个方法:
protected ActionForward processActionPerform(HttpServletRequest request,
HttpServletResponse response, Action action, ActionForm form,
ActionMapping mapping)
throws IOException, ServletException {
try {
return (action.execute(mapping, form, request, response));
} catch (Exception e) {
return (processException(request, response, e, form, mapping));
}
}
调用的是上边的第二个 execute 。把自己的 PageAction 中的 execute改了就能执行到 execute 了。终于搞定了。在网上看到有好多人都有这个问题,不知道大家和我遇到的情况一样不。
总结一下吧,学习还是不能急,遇到问题自己要慢慢找答案,这样能记得更牢,还有就是寻找错误要按照一定的步骤来,不能瞎找。第一次写博客,写得不怎么样,光贴代码了,呵呵。