struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="SOIndex" extends="struts-default" namespace="/">
<action name="index" >
<result> /WEB-INF/jsp/SOLoginPage.jsp </result>
</action>
</package>
<package name="SOLogin" extends="struts-default" >
<action name="login" class="com.azure.action.SOLoginAction"
method="execute" >
<result name="success"> /WEB-INF/jsp/SOBookStore.jsp </result>
<result name="failure"> /WEB-INF/jsp/SOLoginPage.jsp </result>
</action>
</package>
</struts>
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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name> SO Book Store Web Application </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>
</web-app>
动作.java
package com.azure.action;
public class SOLoginAction {
private String username;
private String password;
private String errorMsg;
public String execute() throws Exception {
String mapping = "";
if( (this.username.equalsIgnoreCase("JE90") && this.password.equals("admin")) ) {
mapping = "success";
errorMsg = "";
}
else {
mapping = "failure";
errorMsg = "Incorrect username or password.\nReview credentials and try again.";
}
return mapping;
}
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 getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
}
SOLoginPage.jsp
<%@ page language="java" contentType="text/html; charset=US-ASCII"
pageEncoding="US-ASCII"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=US-ASCII">
<title>SO Book Store Login Page! </title>
</head>
<body>
<font color="red"><s:property value="errorMsg" /></font>
<s:form action="login" method="post">
<s:textfield name="username" label="Username" />
<s:password name="password" label="Password" />
<s:submit value="Login" />
</s:form>
</body>
</html>
SOBookStore.jsp
<%@ page language="java" contentType="text/html; charset=US-ASCII"
pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title> SO Book Store </title>
</head>
<body>
<h1> Login Successful <s:property value="username" />! </h1>
</body>
</html>
这些是我目前正在处理的所有文件。我现在遇到的问题是,无论我将用户名和密码多么错误,struts2 都不会将我发送到带有更新的 errorMsg 数据的登录页面。我已经检查了整个网络,并且没有任何帖子出现,甚至可以远程让我找到这个应用程序出了什么问题......非常感谢任何指向正确方向的帮助。以前我在 struts 1.3.10 中执行此操作,但我认为它太旧了,无法与 tomcat8 一起玩,所以我切换到 2.3.4。我在 struts 1 和 2 之间的过渡中遗漏了什么吗?
使用 Struts 2.3.4 Tomcat8 Java 1.7
更新-----所以我遇到了一个问题,struts2 拒绝看到所谓的重写方法执行(有或没有 method="xxx" 存在)或我在这里创建的任何自定义方法是更新的 struts.xml
<struts>
<constant name="struts.devMode" value="true" />
<package name="LoginAction" namespace="/jsp" extends="struts-default">
<action name="login" class="com.azure.action.SOlLoginAction" method="authenticate">
<result name="loginSuccess"> /jsp/SOBookStore.jsp </result>
<result name="loginFail"> /jsp/SOLoginPage.jsp </result>
</action>
</package>
</struts>
我现在可能做错了什么?我知道这是可能的,因为我看到其他人使用他们自己的方法而不是覆盖执行。我是否需要告诉 struts 使用自定义方法?PS我只是将jsp文件移到WEB-INF文件夹之外。不知道为什么这让我如此烦恼。