工作需要,初学Struts,弄了个简单的注册例子都出毛病,郁闷死。大家有空的话帮着看看,谢谢!
问题是:注册页面可以正常显示,但是点注册按钮,页面没有跳转Success.jsp或Failure.jsp页面,而是到了一个空页面。我觉得根本就没有进入到Action里执行,因为我曾在Actrion里放了一个打印语句,没有结果。以下是源码。
Action
package app;import org.apache.struts.action.*;import javax.servlet.http.*;public class RegisterAction extends Action { public ActionForward excute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) { // ○1 将form 转型为RegisterForm RegisterForm rf = (RegisterForm) form; String username = rf.getUsername(); String password1 = rf.getPassword1(); String password2 = rf.getPassword2(); ActionForward forward = new ActionForward(); // ○2 应用业务逻辑 if (password1.equals(password2)) { forward = mapping.findForward("success"); } else { forward = mapping.findForward("failure"); } // ○4 返回针对failure的ActionForward return (forward); }}
package app;
import org.apache.struts.action.*;
public class RegisterForm extends ActionForm {
/**
*
*/
private static final long serialVersionUID = 7778795904157172654L;
private String password2;
private String password1;
private String username;
public String getUsername() {
return username;
}
public void setUsername(String i) {
username = i;
}
public String getPassword1() {
return password1;
}
public void setPassword1(String i) {
password1 = i;
}
public String getPassword2() {
return password2;
}
public void setPassword2(String i) {
password2 = i;
}
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<HTML>
<HEAD>
<TITLE>register </TITLE>
</HEAD>
<BODY>
<P>
<html:form action="/register.do">
用户名: <html:text property="username"> </html:text>
<BR>
密码1: <html:text property="password1"> </html:text>
<BR>
密码2: <html:text property="password2"> </html:text>
<BR>
<html:submit value="Register"> </html:submit>
</html:form>
</P>
</BODY>
</HTML>
<?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> <form-bean name="RegisterForm" type="app.RegisterForm"> </form-bean> </form-beans> <action-mappings> <action path="/register" type="app.RegisterAction" name="RegisterForm" scope="request" input="/register.jsp"> <forward name="success" path="/Success.jsp"></forward> <forward name="failure" path="/Failure.jsp"></forward> </action> </action-mappings> <message-resources parameter="com.yourcompany.struts.ApplicationResources" /></struts-config>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML> <HEAD> <TITLE>aaa</TITLE> </HEAD> <BODY> <P> aaaaa </P> <P> <A href="register.jsp">aaaaaaa</A> </P> </BODY></HTML>
return mapping.findForward("success");
[解决办法]
<forward name="success" path="/Success.jsp"></forward>
<forward name="failure" path="/Failure.jsp"></forward>
你看下这里的页面前面的/去掉行不行,如果确认都没错,那就把tomcat停下,
然后删除work目录下的东西,再重新启动,访问
[解决办法]
既然 运行后url是http://localhost:8080/register/register.do ,那么就证明已经进入了action,
你的配置没有错误。
但是 你出现了空白页,那只能说明你的forward有问题,按照你的配置:
<forward name="success" path="/Success.jsp"> </forward>
<forward name="failure" path="/Failure.jsp"> </forward>
也就是多,你的Success.jsp路径不对,请确定你的当前路径,你的action path="/register" 而你的页面url为:http://localhost:8080/register/register.do 也就是说你的上下文径为:http://localhost:8080/register。
你把你的jsp文件放到register文件夹下试试 或者 更改你的应用程序上下文。
[解决办法]
action="<%=request.getContextPath()%>/register.do" 试试
[解决办法]
ActionForward forward = new ActionForward();
这段代码有错,别new,直接ActionForward forward=null。就可以了。
[解决办法]
action=" <%=request.getContextPath()%>/register.do" 试试
[解决办法]
public ActionForward [color=#FF0000]excute[/color](ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res)