欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Struts2 -- No result defined for action action.Register and result success

程序员文章站 2022-05-24 09:16:47
...

Struts2 – No result defined for action action.Register and result success


Struts2 -- No result defined for action action.Register and result success
  在跟着官网的例子学习Struts 2框架的时候遇到了个问题:No result defined for action action.Register and result success.最后发现是struts.xml里 package的namespace的问题。解决方法在struts.xml的注释里说明了。

Person.java:

package action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import model.Person;

/**
 * Create by sino_crazy_snail on 2018-04-02
 * Email: [email protected]
 */
public class Register extends ActionSupport {
    private Person person;

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    /**
     * A default implementation that does nothing an returns "success".
     * <p>
     * <p>
     * Subclasses should override this method to provide their business logic.
     * </p>
     * <p>
     * <p>
     * See also {@link Action#execute()}.
     * </p>
     *
     * @return returns {@link #SUCCESS}
     * @throws Exception can be thrown by subclasses.
     */
    @Override
    public String execute() throws Exception {

        return SUCCESS;
    }
}

struts.xml:

<?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE struts PUBLIC
                "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
                "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>

    <constant name="struts.devMode" value="true" />
    <constant name="struts.convention.classes.reload" value="true"/>
    <!--换成"<package name="hellodemo" namespace="/" extends="struts-default">"就可以正常运行了-->
    <package name="hellodemo"  extends="struts-default">
        <action name="index">
            <result>/index.jsp</result>
        </action>

        <action name="register" class="action.Register" method="execute">
            <result name="success">/thankyou.jsp</result>
        </action>
    </package>
</struts>

index.jsp:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  <title>Hello_World Application - Welcome</title>
</head>
<body>
  <h1>Welcome To Struts 2!</h1>
    <p><a href="register.jsp">Please register</a> for our prize drawing. </p>
</body>
</html>

register.jsp

<%--
  User : sino_crazy_snail
  Email: [email protected]
  Date : 2018-04-02
  Time : 11:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Register</title>
</head>
<body>
    <h3>Register for a prize by completing this form.</h3>
    <s:form action="register">
        <s:textfield name="person.firstName" label="First Name: "/>
        <s:textfield name="person.lastName" label="Last Name: "/>
        <s:textfield name="person.email" label="Email: "/>
        <s:textfield name="person.age" label="Age: "/>
        <s:submit value="Submit"/>
    </s:form>
</body>
</html>

thankyou.jsp

<%--
  User : sino_crazy_snail
  Email: [email protected]
  Date : 2018-04-02
  Time : 12:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Thank You</title>
</head>
<body>
    <h3>Thank you for registering for a prize.</h3>
    <p>Your registration information: <s:property value="person"/> </p>
    <p><a href="<s:url action='index'/>">Return to home page</a> </p>
</body>
</html>

build.gradle:

group 'sino_crazy_snail'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'war'

sourceCompatibility = 1.8
buildscript {
    ext.struts2_version="2.5.16"
    ext.log4j2_version="2.11.0"
}
repositories {
    mavenCentral()
}

dependencies {
    compile "org.apache.struts:struts2-core:${struts2_version}"
    compile "org.apache.struts:struts2-convention-plugin:${struts2_version}"
    compile "org.apache.logging.log4j:log4j-core:${log4j2_version}"
    compile "org.apache.logging.log4j:log4j-api:${log4j2_version}"
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

Struts2 -- No result defined for action action.Register and result success