我正在尝试一个简单的struts应用程序,它将一个String值从java类文件传递给jsp。
这里是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>
<form-bean name="myForm" type="com.myForm" />
</form-beans>
<action-mappings>
<action path="/view" scope="request" name="myForm" type="com.myAction" validate="false">
<forward name="success" path="/first" />
</action>
<action path="/view"
forward="/view.jsp"/>
<action path="/first"
forward="/first.jsp" />
</action-mappings>
</struts-config>
view.jsp的
<!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
package com;
import javax.servlet.http.HttpServletRequest;
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
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;
public class myAction extends org.apache.struts.action.Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
myForm form1=(myForm) form;
String s="Karthikeyan";
System.out.println("Hello "+s);
form1.setSt(s);
//RequestDispatcher reqDispatcher = getRequestDispatcher("view.jsp");
//reqDispatcher.forward(request,response);
return (mapping.findForward("success"));
}
}
即使我已经为类文件设置了类路径,但在编译myAction.java时出现以下错误 -
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;
^
当我尝试在Web浏览器上执行应用程序时,出现以下错误 -
org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot find bean: "myForm" in any scope
我坚持这个问题很长一段时间没有任何解决方案。请帮帮我。
答案 0 :(得分:0)
你错过了myform class的getter和setter方法。请在myAction类中为myform类添加getter和setter方法,并在web.xml中添加过滤器。
答案 1 :(得分:0)
创建一个新项目,设置类路径,执行它,它将工作:)。它对我有用。