我是struts的新手,我一直在尝试构建一个简单的struts应用程序。我有错误,我过去几天一直试图解决,但无法解决它。以下是我使用的文件 -
的web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!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>
<display-name>Hello World Struts Application</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>/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>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>view.jsp</welcome-file>
</welcome-file-list>
</web-app>
的struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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>
<bean name="myForm" type="com.myForm" />
</form-beans>
<action-mappings>
<action path="/view" name="myForm" type="com.myAction" validate="false">
<forward name="success" path="/first" />
</action>
<action path="/view"
forward="/view.jsp"/>
<action path="/first" type="com.myAction" validate="false">
<forward name="success" path="/first.jsp" />
</action>
</action-mappings>
</struts-config>
view.jsp的
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="first.do">
Enter name :
<input type="text" name="name"/>
<input type="submit" value="Enter"/>
</form>
</body>
</html>
first.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
Welcome!!!!!!!!
<bean:write name="myForm" property="st"/>
</body>
</html>
myForm.java(Bean类) -
package com;
public class myForm extends org.apache.struts.action.ActionForm {
private String st;
public String getSt()
{
return st;
}
public void setSt(String st)
{
this.st=st;
}
public myForm(String st)
{
this.st=st;
}
}
myAction.java(Action class) -
package com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.myForm;
public class myAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
myForm form1=(myForm) form;
String s="Karthikeyan";
form1.setSt(s);
//RequestDispatcher reqDispatcher = getRequestDispatcher("view.jsp");
//reqDispatcher.forward(request,response);
return (mapping.findForward("success"));
}
}
当我编译myAction.java时,它会出现以下错误 -
myAction.java:11: cannot find symbol
symbol : class myForm
location: package com
import com.myForm;
^
myAction.java:18: cannot find symbol
symbol : class myForm
location: class com.myAction
myForm form1=(myForm) form;
^
myAction.java:18: cannot find symbol
symbol : class myForm
location: class com.myAction
myForm form1=(myForm) form;
^
此外,当我运行tomcat服务器时,我收到以下错误 -
org.apache.commons.digester.Digester error
SEVERE: Parse Error at line 9 column 43: Element type "bean" must be declared.
和
org.apache.commons.digester.Digester error
SEVERE: Parse Error at line 10 column 15: The content of element type "form-beans" must match "(form-bean)*".
请帮帮我。
答案 0 :(得分:0)
你写了错误的struts-config.xml,你应该改变form-beans格式如下:
<form-beans>
<form-bean name="myForm" type="com.myForm"></form-bean>
</form-beans>