我是Struts的新手,我遵循了如何创建Struts Web应用程序的教程。这是我创建的文件。
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Mon application Struts de tests</display-name>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-config.xml</param-value>
</init-param>
<load-on-startup>1</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>
</web-app>
的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="HelloWorldForm" type="com.hello.HelloWorldFrom" />
<form-bean name="LoginForm" type="com.test.LoginForm.LoginForm"/>
</form-beans>
<action-mappings type="org.apache.struts.action.ActionMapping">
<action path="/helloWorld" type="com.hello" name="HelloWorldForm">
<forward name="success" path="/HelloWorld.jsp" />
</action>
<action path="login" parameter="" input="/index.jsp" scope="request"
name="loginForm" type="com.test.controller.LoginAction">
<forward name="succes" path="/accueil.jsp" redirect="false" />
<forward name="echec" path="/index.jsp" redirect="false" />
</action>
</action-mappings>
的index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page language="java" %>
<%@ 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" "http://www.w3.org/TR/html4/loose.dtd">
<html:html locale="true">
<head>
<title>authentication</title>
<html:base/>
</head>
<body bgcolor="white">
<html:form action="login" focus="nameUserr">
<table border="0" align="center">
<tr>
<td align="right">
User:
</td>
<td align="left">
<html:text property="nameUserr" size="20" maxlength="20"/>
</td>
</tr>
<tr>
<td align="right">
Password :
</td>
<td align="left">
<html:password property="psdUser" size="20" maxlength="20"
redisplay="false"/>
</td>
</tr>
<tr>
<td align="right">
<html:submit property="submit" value="Submit"/>
</td>
<td align="left">
<html:reset/>
</td>
</tr>
</table>
</html:form>
LoginAction.java
package com.test.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;
import com.test.LoginForm.LoginForm;
public class LoginAction extends Action{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
HttpServletResponse res) throws Exception {
String result = null;
String nomUtilisateur = ((LoginForm) form).getNameUser();
String mdpUtilisateur = ((LoginForm) form).getPsdUser();
if (nameUser.equals("xyz") && psdUser.equals("xyz")) {
result = "succes";
} else {
result = "failure";
}
return mapping.findForward(result);
}
}
LoginForm.java
package com.test.LoginForm;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class LoginForm extends ActionForm{
String nameUser;
String psdUser;
public String getpsdUser() {
return psdUser;
}
public void setpsdUser(String psdUser) {
this.psdUser= psdUser;
}
public String getnameUser() {
return nameUser;
}
public void setnameUser(String nameUser) {
this.nameUser= nameUser;
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
return errors;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.psdUser= null;
this.nameUser= null;
}
}
但在运行时,我收到以下错误
Etat HTTP 500 - javax.servlet.ServletException:javax.servlet.jsp.JspException:找不到ActionMappings或ActionFormBeans集合
编辑:完整的堆栈跟踪
Report type Exception
Message javax.servlet.ServletException: javax.servlet.jsp.JspException: Can not find ActionMappings gold ActionFormBeans collection
description The server encountered an internal error that prevented it from fulfilling the request.
exception
org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: Can not find ActionMappings gold ActionFormBeans collection
org.apache.jasper.servlet.JspServletWrapper.handleJspException (JspServletWrapper.java:549)
org.apache.jasper.servlet.JspServletWrapper.service (JspServletWrapper.java:455)
org.apache.jasper.servlet.JspServlet.serviceJspFile (JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service (JspServlet.java:339)
javax.servlet.http.HttpServlet.service (HttpServlet.java:727)
Parent cause
javax.servlet.ServletException: javax.servlet.jsp.JspException: Can not find ActionMappings gold ActionFormBeans collection
org.apache.jasper.runtime.PageContextImpl.doHandlePageException (PageContextImpl.java:916)
org.apache.jasper.runtime.PageContextImpl.handlePageException (PageContextImpl.java:845)
org.apache.jsp.index_jsp._jspService (index_jsp.java:112)
org.apache.jasper.runtime.HttpJspBase.service (HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service (HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service (JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile (JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service (JspServlet.java:339)
javax.servlet.http.HttpServlet.service (HttpServlet.java:727)
Parent cause
javax.servlet.jsp.JspException: Can not find ActionMappings gold ActionFormBeans collection
org.apache.struts.taglib.html.FormTag.lookup (FormTag.java:798)
org.apache.struts.taglib.html.FormTag.doStartTag (FormTag.java:506)
org.apache.jsp.index_jsp._jspx_meth_html_005fform_005f0 (index_jsp.java:189)
org.apache.jsp.index_jsp._jspx_meth_html_005fhtml_005f0 (index_jsp.java:143)
org.apache.jsp.index_jsp._jspService (index_jsp.java:99)
org.apache.jasper.runtime.HttpJspBase.service (HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service (HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service (JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile (JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service (JspServlet.java:339)
javax.servlet.http.HttpServlet.service (HttpServlet.java:727)
Note The complete trace of the mother cause of this error is available in the log files for Apache Tomcat / 7.0.59.
我用谷歌搜索,我尝试了所有提出的解决方案,但错误仍然存在。任何想法请。
P.S:我使用Struts 1.1
答案 0 :(得分:1)
您文件中的struts-config.xml
以</action-mappings>
而不是<struts-config>
每个文件中也有很多错误。因此,您似乎没有尝试验证您的代码是否正确。
1)在web.xml
档案
DTD声明存在问题,应该是:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
2)在struts-config.xml
档案中:
您已将表单bean名称声明为LoginForm
,而action
正在使用loginForm
此外,没有forward
声明显示您的操作类返回failure
时要执行的操作。
所以代码应该是这样的:
<action path="/login" parameter="" input="/index.jsp" scope="request"
name="LoginForm" type="com.test.controller.LoginAction">
<forward name="succes" path="/accueil.jsp" redirect="false" />
<forward name="echec" path="/index.jsp" redirect="false" />
<forward name="failure" path="/loginFailed.jsp" redirect="false" />
</action>
3)在index.jsp
中,您有多个问题:
a)taglib声明应该是这样的(对于Struts 1.3.10):
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="logic" %>
b)struts html自定义标记没有任何名为locale
的属性,index.jsp也表明你没有关闭struts html标记。
检查一下:
<html:html>
......
</html:html>
c)财产声明:
<html:text property="nameUserr" size="20" maxlength="20"/>
表单bean有一个名为nameUserr
的属性,但LoginForm
的属性声明为nameUser
,所以将jsp文件中的代码更改为
<html:text property="nameUser" size="20" maxlength="20"/>
4)LoginForm bean有错误的getter和setter方法。他们应该像:
public String getNameUser() {
return nameUser;
}
public void setNameUser(String nameUser) {
this.nameUser = nameUser;
}
public String getPsdUser() {
return psdUser;
}
public void setPsdUser(String psdUser) {
this.psdUser = psdUser;
}
5)最后在LoginAction
动作类中,你的比较不正确,应该是:
String nomUtilisateur = ((LoginForm) form).getNameUser();
String mdpUtilisateur = ((LoginForm) form).getPsdUser();
if (nomUtilisateur.equals("xyz") && mdpUtilisateur.equals("xyz")) {
result = "succes";
} else {
result = "failure";
}