0

这些是我与AccessDenied.jspand一起创建的文件HelloWorld.jsp,但代码没有运行。

package com.Struts;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldActionSupport extends ActionSupport {
 private String name;

public HelloWorldActionSupport() {
}

@Override
public String execute() throws Exception
{
    if("SECRET".equals(name))
    {
        return SUCCESS;
    }
    else
    {
        return ERROR;
    }
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

Struts.xml文件:

    <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

    <struts>
    <!-- Configuration for the default package.
    <constant name="struts.devMode" value="true"/>
    <package name="hello" extends="struts-default">
    <action name="helloWorldActionSupport" class= "com.Struts.HelloWorldActionSupport">
    <result name="success">/HelloWorld.jsp</result>
    <result name="error">/AccessDenied.jsp</result>
    </action>
    </package>
    </struts>

index.jsp文件:

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@taglib prefix="s" uri="/struts-tags" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Hello World</title>
    </head>
    <body>
    <h1>Hello World from Struts-2</h1>
    <s:action name="helloWorldActionSupport" executeResult="true">
    <label for="name">Please Enter Your Name: </label><br/>
    <input type="text" name="name"/>
    <input type="submit" value="Say Hello"/>
    </s:action>
    </body>
    </html>

单击提交按钮后,它不会进入下一页。

4

1 回答 1

1

使用此代码

<s:form action="helloWorldActionSupport" method="POST">
  <label for="name">Please Enter Your Name: </label><br/>
  <input type="text" name="name"/>
  <input type="submit" value="Say Hello"/>
</s:form>
于 2013-06-09T11:41:44.777 回答