无法使用struts在jsp中创建Hello World

时间:2013-09-04 07:52:25

标签: jsp struts

我想使用jsp和struts 1来做一个helloWorld应用程序,我已经接受了一些教程,但我不能让它运行起来。我不知道我做错了什么可以帮助我?这是代码:

的index.jsp

<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<logic:redirect forward="helloWorld"/>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns  /javaee/web-app_2_5.xsd"
     id="WebApp_ID" version="2.5">

<display-name>struts</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>2</load-on-startup>
</servlet>

<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>

的helloWorld.jsp

<%@ 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-logic" prefix="logic" %>

<html>
<head>
    <title>Hello World</title>
</head>
<body>

    <html:form action="HelloWorldForm">
        <bean:write name="HelloWorldForm" property="message" />
    </html:form>

</body>
</html>

的struts-config.xml

 <?xml version="1.0" encoding="UTF-8" ?>

<!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>

<form-beans>
    <form-bean name="helloWorldForm" 
               type="com.vaannila.form.HelloWorldForm"/>
</form-beans>

<global-forwards>
    <forward name="helloWorld"
             path="/helloWorld.do"/>
</global-forwards>

<action-mappings>
    <action               
            path="/helloWorld"
            type="com.vaannila.action.HelloWorldAction"
            scope="request"
            name="helloWorldForm">
        <forward name="success" path="/helloWorld.jsp" />

    </action>    
</action-mappings>

</struts-config>

HelloWorldForm.java

package com.vaannila.form;
import org.apache.struts.action.ActionForm;

public class HelloWorldForm extends ActionForm {

private static final long serialVersionUID = 1L;    
private String message;

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}   
}

HelloWorldAction.java

package com.vaannila.action;

import com.vaannila.form.HelloWorldForm;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorldAction extends Action {

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
HelloWorldForm helloWorld = (HelloWorldForm) form;
helloWorld.setMessage("Hello World");

return mapping.findForward("success");
}
}

以下是我得到的例外情况:

javax.servlet.ServletException: org.apache.jasper.JasperException: An exception occurred processing JSP page /helloWorld.jsp at line 15

12: 
13:  Hello
14: 
15: <html:form action="HelloWorldForm">
16: <bean:write name="HelloWorldForm" property="message" />
17: </html:form>
18: 


Stacktrace:
    org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:286)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

org.apache.jasper.JasperException: An exception occurred processing JSP page /helloWorld.jsp at line 15

12: 
13:  Hello
14: 
15: <html:form action="HelloWorldForm">
16: <bean:write name="HelloWorldForm" property="message" />
17: </html:form>
18: 


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.struts.chain.commands.servlet.PerformForward.handleAsForward(PerformForward.java:113)
    org.apache.struts.chain.commands.servlet.PerformForward.perform(PerformForward.java:96)
    org.apache.struts.chain.commands.AbstractPerformForward.execute(AbstractPerformForward.java:54)
    org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
    org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
    org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot retrieve mapping for action: "/HelloWorldForm"
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
    org.apache.jsp.helloWorld_jsp._jspService(helloWorld_jsp.java:95)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.struts.chain.commands.servlet.PerformForward.handleAsForward(PerformForward.java:113)
    org.apache.struts.chain.commands.servlet.PerformForward.perform(PerformForward.java:96)
    org.apache.struts.chain.commands.AbstractPerformForward.execute(AbstractPerformForward.java:54)
    org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
    org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
    org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

javax.servlet.jsp.JspException: Cannot retrieve mapping for action: "/HelloWorldForm"
    org.apache.struts.taglib.html.FormTag.lookup(FormTag.java:862)
    org.apache.struts.taglib.html.FormTag.doStartTag(FormTag.java:488)
    org.apache.jsp.helloWorld_jsp._jspx_meth_html_005fform_005f0(helloWorld_jsp.java:113)
    org.apache.jsp.helloWorld_jsp._jspService(helloWorld_jsp.java:83)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.struts.chain.commands.servlet.PerformForward.handleAsForward(PerformForward.java:113)
    org.apache.struts.chain.commands.servlet.PerformForward.perform(PerformForward.java:96)
    org.apache.struts.chain.commands.AbstractPerformForward.execute(AbstractPerformForward.java:54)
    org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
    org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
    org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

1 个答案:

答案 0 :(得分:0)

您的表单操作指向错误的位置, 正确的方法应该是

15: <html:form action="/helloWorld.do">
16: <bean:write name="HelloWorldForm" property="message" />
17: </html:form>