struts2 bug(There is no Action mapped for namespace / and action name login.)

最近工作不忙,看了看Struts2,写了一个简单的登录例子,出现了一个bug,

我也在网上查了查,但没结果,所以请各位高手们看看,给给意见了。

 

以下就是bug

警告: Could not find action or result
There is no Action mapped for namespace / and action name login. - [unknown location]
    at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:178)
    at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
    at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
    at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478)
    at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)

 

jar包包括:

commons-fileupload-1.2.1.jar

commons-io-1.3.2.jar

commons-logging-1.0.4.jar

freemarker-2.3.15.jar

ognl-2.7.3.jar

struts2-core-2.1.8.1.jar

xwork-core-2.1.6.jar

 

index.jsp页面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>login jsp</title>
</head>
<body>
    <form action="login.action" method="post">
        userName : <input type="text" name="userName" /><br/>
        password : <input type="text" name="password" /><br/>
        <input type="submit" value="submit" />
    </form>
</body>
</html>

 

success.jsp页面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>success jsp</title>
</head>
<body>
     login success!
</body>
</html>

 

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>Strutsps</display-name>
 
  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>*.action</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>

 

struts.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
    "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <package name="base" extends="struts-default">
        <action name="login" class="com.gxf.struts.action.LoginAction">
            <result name="success">success.jsp</result>
            <result name="index">index.jsp</result>
        </action>
    </package>
</struts>

 

LoginAction.java

package com.gxf.struts.action;

public class LoginAction {
    private String userName;
    private String password;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String execute() throws Exception {
        if ("admin".equals(userName) && "admin".equals(password)) {
            return "success";
        } else {
            return "index";
        }
    }
}

 

帮我看看是啥问题?

谢谢!

11个回答

正常的话,只要放在classes目录下面就可以自动加载上的。

如果没有放到这个目录下面,你建立的不是一个web工程,你试着建立一个web工程,或者手工在WEB-INF中建立一个classes目录,将需要的类和配置文件拷贝到这个目录下面,重启应用看看。

试试base/login.action.

映射的login在默认的namespace中找不到,试试上面的吧。

[code="xml"]
success.jsp
index.jsp
[/code]

success.jsp 前面加上/

<filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

你把映射改成/*.应该是这块的问题

那可能是因为配置文件没有正确加载导致的,看看struts.xml有没有加载上。

你在两个位置上都加上
[code="xml"]


/success.jsp

/index.jsp


[/code]

没有classes 的话 点project -> clean 然后在重新启动项目build working set就好了

你看你的 struts.xml放在什么位置 Struts 2 looks for struts.xml in the base class directory

你这么的 方法中只写一个return
[code="java"] public String execute() throws Exception {
system.out.println("输出一句话");
return "success";
}
}[/code]
[code="xml"]
action中也配置一个


/success.jsp



[/code]

看看这回好使不

共11条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!