无法在Struts2.5.16中调用操作

时间:2018-04-09 01:22:56

标签: struts2

Project Structure in Eclipse

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>Struts2Starter</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

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>
    <package name="default" extends="struts-default">
        <action name="getTutorial" class="org.awanish.action.TutorialAction">
                <result name="success">/success.jsp</result>
                <result name="error">/error.jsp</result>
        </action>
    </package>
</struts>

ActionClass

package org.awanish.action;

public class TutorialAction {
    public String execute() {
        System.out.println("Hello from execute");
        return "success";
            }

}

在tomcat-8.0.43容器中运行上述项目时,获取我的welecome页面,但是当尝试在浏览器http://localhost:8080/Struts2Starter/getTutorial.action中测试操作时,它会给出这个

Error in browser

1 个答案:

答案 0 :(得分:1)

1.check in logs if:Hello from execute is printed
2. check if the jsp exists in the correct path
3. .action is not required at the end
4.<action name="getTutorial" class="org.awanish.action.TutorialAction" method="define your own method and try calling it">
5.Add jars at runtime also inside WEB-INF/lib folder

 http://localhost:8080/Struts2Starter/getTutorial

And if you begineer try using struts 2.3 rather than struts 2.5 ,you will get lots of example:https://www.mkyong.com/struts2/struts-2-hello-world-example/

Download code and build on top of it if still facing issue