我正在尝试编写一个登录示例,如果用户名等于密码则将用户带到success.jsp,如果没有则写入错误消息。错误消息有效但成功(用户名=密码)导致404(HTTP状态404 - /Struts2Ch4/success.jsp)
我在上一篇文章中遇到过类似的问题。我没有在类中声明RESULT参数。我被建议在类文件中使用注释,这解决了这个问题。在这种情况下,成功和结果参数似乎都是由类声明的,所以我不确定为什么我会得到404.
alias.jsp
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title><s:text name="app.title"/></title>
<link rel="stylesheet" href="mystyle.css" type="text/css" />
</head>
<body>
<table align="center" width="300">
<tr>
<td colspan="2"><s:actionerror/></td>
</tr>
<tr><td align="center" colspan="2">Enter Login ID and Password</td></tr>
<tr><td align="center">
<s:form action="aliasing" method="post">
<s:textfield name="uname" key="app.loginid"/>
<s:password name="pwd" key="app.password"/>
<s:submit value="Enter"/>
</s:form>
</td>
</tr>
<tr><td align="center" colspan="2">
<a href="index.jsp">B a c k</a></td>
</tr>
</table>
</body>
</html>
struts.xml中
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="exception"/>
</global-exception-mappings>
<action name="aliasing" class="com.manaar.action.AliasAction">
<param name="aliases">#{ 'uname' : 'loginid','pwd' : 'password' }</param>
<interceptor-ref name="alias"/>
<interceptor-ref name="basicStack"/>
<result name="success">/success.jsp</result>
<result name="error">/alias.jsp</result>
<result name="input">/alias.jsp</result>
</action>
</package>
</struts>
AliasAction.java
package com.manaar.action;
import com.opensymphony.xwork2.ActionSupport;
public class AliasAction extends ActionSupport {
private String loginid;
private String password;
public String getLoginid() {
return loginid;
}
public void setLoginid(String loginid) {
this.loginid = loginid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() throws Exception {
System.out.println("Login Id: "+getLoginid());
System.out.println("Password: "+getPassword());
if(loginid.equals(password))
return SUCCESS;
else{
this.addActionError(getText("app.invalid"));
return ERROR;
}
}
}
的success.jsp
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title><s:text name="app.title"/></title>
<link rel="stylesheet" href="mystyle.css" type="text/css" />
</head>
<body>
<div align="center">
<h1><s:text name="app.success"/></h1>
The action has returned SUCCESS as result code.<br><br>
<a href="index.jsp">Back to Index</a></div>
</body>
</html>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org /2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com /xml/ns/javaee http://java.sun.com/xml/ns/javaee /web-app_3_0.xsd">
<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>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
的index.jsp
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"/>
<html>
<head><title><s:text name="app.title"/></title>
<link rel="stylesheet" href="mystyle.css" type="text/css" />
</head>
<body>
<table align="center" width=400>
<tr><td><h2>Sruts 2 Interceptors</h2></td></tr>
<tr><td><s:a href="alias.jsp">Interceptor Example 1</s:a></td></tr>
<tr><td>alias, basicStack </td></tr>
<tr><td> </td></tr>
<tr><td><s:a href="model.jsp">Interceptor Example 2</s:a></td></tr>
<tr><td>
exception, prepare, debugging, model-driven, params, conversionError, workflow
</td></tr>
<tr><td> </td></tr>
<tr><td>
<s:a href="servletAction.action">Interceptor Example 3</s:a>
</td></tr>
<tr><td>servlet-config, scoped-model-driven </td></tr>
<tr><td> </td></tr>
<tr><td><s:a href="longAction.action">Interceptor Example 4</s:a></td></tr>
<tr><td>completeStack, execAndWait</td></tr>
<tr><td> </td></tr>
<tr><td><s:a href="login.jsp">Interceptor Example 5</s:a> </td></tr>
<tr><td>basicStack, validation, workflow, scope </td></tr>
</table>
</body>
</html>