【发布时间】:2021-03-25 16:04:48
【问题描述】:
这是 Struts 程序。我必须制作一个登录页面。如果我在登录页面输入名称itcast 和密码123,则页面转到success.jsp(我在此页面失败),否则显示error.jsp。
loginaction.java:
package cn.itcast.action;
import com.opensymphopensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
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;
}
//@Override
public String execute() throws Exception {
ActionContext context = ActionContext.getContext();
if ("itcast".equals(username) && "123".equals(password)) {
context.put("username", username);//${username}<br>
context.put("password", password); //${password}
context.put("success", "success");
return SUCCESS;
} else {
context.put("error", "error_zdh");
return ERROR;
}
}
}
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- <constant name="struts.enable.DynamicMethodInvocation" value="false"
/> -->
<!-- <constant name="struts.devMode" value="true" /> -->
<package name="default" extends="struts-default">
<action name="login" class="cn.itcast.action.LoginAction">
<result name="success" type="redirect">/success.jsp</result>
<result name="error" type="dispatcher">/error.jsp</result>
</action>
</package>
</struts>
login.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>login page</title>
</head>
<body>
<div align="center">login page
<form action="login" method="post">
username:<input type="text" name="username" /><br>
password:<input
type="password" name="password" /><br> <input type="submit"
value="login" />
</form>
</div>
</body>
</html>
error.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>failed page</title>
</head>
<body>
<p>
${error }<br>
</p>
</body>
</html>
success.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>success page</title>
</head>
<body>
<p>
${success }<br>
</p>
<h2>login information</h2>
username:${username }<br>
password:${password }<br>
</body>
</html>
【问题讨论】:
-
你为什么不调试
execute看看发生了什么? -
您好!如果您解释您尝试过的内容以及代码如何无法按预期工作,则 StackOverflow 效果最佳。感谢您提供您的代码,但尚不清楚您的实际问题是什么。您能否更具体地说明您希望代码如何工作?
-
您正在重定向,这意味着有一个新请求,这意味着有一个新操作,这意味着您在任何地方都不再有
username或password。