学习sturts2时出现的不能加载action问题

来源:互联网 发布:域名备案流程 编辑:程序博客网 时间:2024/05/30 23:33

前几天学习struts2时遇到一个比较苦恼的问题,之前解决了,但是希望更多像我一样的人可以解决,所以把问题发表出来。

在搭建struts2环境时做了一个简单的小测试,没有写index.jsp页面,只写了一个action类,运行tomcat时没有报严重异常,然后直接打开 http://localhost:8080/MySpring4_ssh/UserAtion.action

结果出现这个问题:

配上我的类文件和配置文件:

Useraction类:

package com.test.action;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport{       public UserAction() {System.out.println("action.......");}@Override    public String execute() throws Exception {    // TODO Auto-generated method stub    return NONE;    }}

struts.xml文件:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><package name="demo1" extends="struts-default" namespace="/"><action name="userAction" class="com.test.action.UserAction"></action></package></struts>

web.xml文件:

<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>MySpring4_ssh</display-name>    <filter>     <filter-name>struts2</filter-name>     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>  <filter-mapping>     <filter-name>struts2</filter-name>     <url-pattern>/*</url-pattern>  </filter-mapping>    <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list></web-app>


检查很多遍这几个文件都没有错,结果很苦恼,千辛万苦百度尝试了很多种方法,结果找到方法:

在sturts.xml文件的<action>上加入这样的一句话:

<default-action-ref name="userAction" />
这句话的意思是如果在web.xml文件的welcome-file中定义的文件找不到,就会交给default-action-ref处理。自动跳转到userAction


问题就解决了。原因我也不太理解,Baidu不出答案,但是我觉得,结合以前学习jsp/servlet时候的问题,直接访问WEB-INF里的jsp文件也会报错,应该是现在新版eclipse,运行web项目时会先执行welcome-file的文件,找不到的话就会报错。


原创粉丝点击