struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="LoginForm"
type="com.neusoft.struts.form.LoginForm" />
</form-beans>
<global-exceptions />
<global-forwards >//全局跳转,找到相应的Action
<forward name="success" path="/success.do" /> //相对路径相对于jsp文件夹的路径
<forward name="error" path="/error.do"/>
</global-forwards>
<action-mappings >
<action
path="/login"
attribute="LoginForm"
name="LoginForm"
scope="request"
type="com.neusoft.struts.action.LoginAction" >
<forward name="success" path="/jsp/success.jsp"/> //FORWARD子元用于经过action处理后的跳转
<forward name="error" path="/jsp/error.jsp"/>
</action>
<action path="/jsp/success" forward="/jsp/success.jsp"/>//FORWARD属性用于直接跳转,不经Action处理,相对路径为struts文件夹
<action path="/jsp/error" forward="/jsp/error.jsp" />
</action-mappings>
<message-resources parameter="com.neusoft.struts.ApplicationResources" />
</struts-config>
jsp页面:
login.jsp:
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>login.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<html:form action="login.do" method="post" focus="login">
<table border="0">
<tr>
<td>Login:</td>
<td><html:text property="name" /></td>
</tr>
<tr>
<td>Password:</td>
<td><html:password property="password" /></td>
</tr>
<tr>
<td colspan="2" align="center"><html:submit /></td>
</tr>
</table>
</html:form>
<a href="success.do">success</a>
<a href="error.do">error</a>
</body>
</html:html>
error.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'error.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
error!!. <br>
</body>
</html>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
success!!!. <br>
</body>
</html>
LoginForm.java
package com.neusoft.struts.form;
import org.apache.struts.action.ActionForm;
public class LoginForm extends ActionForm{ //必须继承ActionForm类
private String name=null;
private String password=null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
LoginAction.java
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.neusoft.struts.action;
import java.io.IOException;
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;
import com.neusoft.struts.form.LoginForm;
/**
* MyEclipse Struts
* Creation date: 07-18-2008
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class LoginAction extends Action {//必须继承Action类
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
// TODO Auto-generated method stub
String back=null;
LoginForm loginform=(LoginForm)form;//ActionForm将LoginForm中的值传过来,必须强制转化
System.out.println(loginform.getName());
if(loginform!=null){ //必须判断,否则抛出空指针异常
if(loginform.getName().equals("sunxf")& loginform.getPassword().equals("sunxf"))
{back="success";}
else {back="error";}
}
return mapping.findForward(back);
}}