HTTP状态404 - 没有映射名称空间[/]的动作和与上下文路径[/ struts2]相关联的动作名称[登录] [英] HTTP Status 404 - There is no Action mapped for namespace [/] and action name [login] associated with context path [/struts2]

查看:479
本文介绍了HTTP状态404 - 没有映射名称空间[/]的动作和与上下文路径[/ struts2]相关联的动作名称[登录]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经学习了Struts2的理论,现在正在练习。在执行项目时遇到问题。我在谷歌搜索的方式很多,但找不到结果。请帮帮我。下面是代码。请帮助我的朋友......

I have learned theory of Struts2 and now practicing. Facing problems while executing project.I searched in Google in many ways but could not find result.Please help me. Below is the code.Please help me friends...

项目结构:

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>struts2</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>login.jsp</welcome-file>
</welcome-file-list>

struts.xml

struts.xml

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

<constant name="resources" value="ApplicationResources" />
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default">
    <action name="login" class="com.practice.structs.actions.LoginAction"
        method="validateUser">
        <result name="success">pages/homepage.jsp</result>
        <result name="error">pages/login.jsp</result>
    </action>

</package>

LoginAction.java

LoginAction.java

package com.practice.structs.actions;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
private String userName;
private String password;
public String validateUser(){
    if(this.userName.equalsIgnoreCase("abc") && this.password.equalsIgnoreCase("abc"))
    {
        return "success";
    }else{
        addActionError(getText("error.login"));
        return "error";
    }
}

/**
 * @return the userName
 */
public String getUserName() {
    return userName;
}
/**
 * @param userName the userName to set
 */
public void setUserName(String userName) {
    this.userName = userName;
}
/**
 * @return the password
 */
public String getPassword() {
    return password;
}
/**
 * @param password the password to set
 */
public void setPassword(String password) {
    this.password = password;
}


}

login.jsp

login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>

<title>Login page</title>
</head>
<body>
<H1><I>Login Page</I></H1>
<s:actionerror />
<s:form action="login.action" method="post">
    <s:textfield name="uname" key="label.username" size="20"/>
    <s:password name="password" key="label.password" size="20"/>
    <s:submit method="execute" key="label.login" align="center"/>
</s:form>
</body>
</html>

homepage.jsp

homepage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<H2><I>Welcome</I></H2>
</body>
</html>


推荐答案

像这样更改你的代码

 <package name="default" namespace="/" extends="struts-default">
        <action name="login" class="com.practice.structs.actions.LoginAction"
            method="validateUser">
            <result name="success">pages/homepage.jsp</result>
            <result name="error">pages/![enter image description here][1]login.jsp</result>
        </action>

    </package>


<s:form action="login" method="post">
    <s:textfield name="uname" key="label.username" size="20"/>
    <s:password name="password" key="label.password" size="20"/>
    <s:submit method="execute" key="label.login" align="center"/>
</s:form>

您的表单中的操作名称是action.login,而在struts.xml中登录都应该是相同的还添加命名空间

your action name in form is action.login and in struts.xml is login both should be same and also add the namespace

这篇关于HTTP状态404 - 没有映射名称空间[/]的动作和与上下文路径[/ struts2]相关联的动作名称[登录]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆