index.jsp<%@ page language="java" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> </head>
<body>
This is my JSP page. <br>
<form action="yjr.do" method="post">
用户:<input type="text" name="username"><br>
<input type="submit" value="登录">
</form>
</body>
</html>-----------------------
login_success.jsp<%@ page language="java" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>login_success.jsp</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> </head>
<body>
<table width="80%" border="0">
<tr>
<td>Hello <%= request.getAttribute("username") %> !!</td>
</tr>
</table>
</body>
</html>------------------------------
login_error.jsp
<%@ page language="java" pageEncoding="ISO-8859-1"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>login_error.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
error!!! <br>
</body>
</html>-------------------------ActionForm1.java
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;public class ActionForm1 extends ActionForm{
private String username=null;public String getUsername() {
return username;
}public void setUsername(String username) {
this.username = username;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.username = null;
}
}--------------------------------
Aciton1.java
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping; public class Action1 extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
// if ("admin".equals(username) && "admin".equals(password)) {
// //转向到登录成功页面
// request.setAttribute("username", username);
// return mapping.findForward("success");
// }else {
// //转向到登录失败页面
// return mapping.findForward("error");
// }
String target = new String("success");
ActionForm1 yy = (ActionForm1)form;
String username = yy.getUsername();
if ( username == null ) {
target =new String("false");
}
else {
request.setAttribute("username", username);
}
return mapping.findForward(target);
}}
-------------------------
struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"><!--
This is a blank Struts configuration file with an example
welcome action/page and other commented sample elements. Tiles and the Struts Validator are configured using the factory defaults
and are ready-to-use. NOTE: If you have a generator tool to create the corresponding Java classes
for you, you could include the details in the "form-bean" declarations.
Otherwise, you would only define the "form-bean" element itself, with the
corresponding "name" and "type" attributes, as shown here.
-->
<struts-config> <form-beans>
<form-bean name="ActionForm1" type="ActionForm1"/>
</form-beans> <action-mappings>
<action
path="/yjr"
type="Action1"
name="ActionForm1"
scope="request"
validate="false"
>
<forward name="success" path="/login_success.jsp"/>
<forward name="false" path="/login_error.jsp"/>
</action>
</action-mappings>
</struts-config>
-----------------------------------
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
-----------------------------------我在文本框中什么也不输入 应该转向 "false" 的 login_error.jsp呀,
可是我文本框为null的时候还是转向了login_success.jsp!!!这是为什么啊,我是初学者,请大家帮我检查下哪儿出了问题??
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> </head>
<body>
This is my JSP page. <br>
<form action="yjr.do" method="post">
用户:<input type="text" name="username"><br>
<input type="submit" value="登录">
</form>
</body>
</html>-----------------------
login_success.jsp<%@ page language="java" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>login_success.jsp</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> </head>
<body>
<table width="80%" border="0">
<tr>
<td>Hello <%= request.getAttribute("username") %> !!</td>
</tr>
</table>
</body>
</html>------------------------------
login_error.jsp
<%@ page language="java" pageEncoding="ISO-8859-1"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>login_error.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
error!!! <br>
</body>
</html>-------------------------ActionForm1.java
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;public class ActionForm1 extends ActionForm{
private String username=null;public String getUsername() {
return username;
}public void setUsername(String username) {
this.username = username;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.username = null;
}
}--------------------------------
Aciton1.java
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping; public class Action1 extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
// if ("admin".equals(username) && "admin".equals(password)) {
// //转向到登录成功页面
// request.setAttribute("username", username);
// return mapping.findForward("success");
// }else {
// //转向到登录失败页面
// return mapping.findForward("error");
// }
String target = new String("success");
ActionForm1 yy = (ActionForm1)form;
String username = yy.getUsername();
if ( username == null ) {
target =new String("false");
}
else {
request.setAttribute("username", username);
}
return mapping.findForward(target);
}}
-------------------------
struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"><!--
This is a blank Struts configuration file with an example
welcome action/page and other commented sample elements. Tiles and the Struts Validator are configured using the factory defaults
and are ready-to-use. NOTE: If you have a generator tool to create the corresponding Java classes
for you, you could include the details in the "form-bean" declarations.
Otherwise, you would only define the "form-bean" element itself, with the
corresponding "name" and "type" attributes, as shown here.
-->
<struts-config> <form-beans>
<form-bean name="ActionForm1" type="ActionForm1"/>
</form-beans> <action-mappings>
<action
path="/yjr"
type="Action1"
name="ActionForm1"
scope="request"
validate="false"
>
<forward name="success" path="/login_success.jsp"/>
<forward name="false" path="/login_error.jsp"/>
</action>
</action-mappings>
</struts-config>
-----------------------------------
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
-----------------------------------我在文本框中什么也不输入 应该转向 "false" 的 login_error.jsp呀,
可是我文本框为null的时候还是转向了login_success.jsp!!!这是为什么啊,我是初学者,请大家帮我检查下哪儿出了问题??
//加上||"".equals(username)
if ( username == null ||"".equals(username)) {
target =new String("false");
}
else {
request.setAttribute("username", username);
}
return mapping.findForward(target);
}