gpt4 book ai didi

java - HTTP 状态 404 - 没有为 namespace [/] 映射的操作和与上下文路径 [/struts2] 关联的操作名称 [登录]

转载 作者:行者123 更新时间:2023-11-30 07:12:03 25 4
gpt4 key购买 nike

我学习了Struts2的理论,现在正在实践中。执行项目时遇到问题。我通过多种方式在谷歌中搜索但找不到结果。请帮助我。下面是代码。请帮助我的 friend ...

项目结构:

Project structure

网络.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

<!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>

登录操作.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;
}


}

登录.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>

主页.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 中是 login 两者应该相同并添加命名空间

关于java - HTTP 状态 404 - 没有为 namespace [/] 映射的操作和与上下文路径 [/struts2] 关联的操作名称 [登录],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20628016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com