1.开发环境
开发之前首先应该准备好开发环境,Eclipse 3.X是一个不错的Java IDE,有很多插件可以供我们选择,服务器用Tomcat就行了。安装好Eclipse和Tomcat之后,还需要两个插件:tomcat 插件和Struts插件,以下是相关下载地址:
Eclipse SDK:
http://www.eclipse.org/downloads/index.php
Struts:
https://sourceforge.jp/projects/amateras/files/ StrutsIDE+htmlEditor,htmlEditer也是必要的
Tomcat :
http://www.sysdeo.com/eclipse/tomcatplugin
Plugin Search:
http://eclipse-plugins.2y.net/eclipse/search.jsp
插件的安装和配置有问题请直接Google。
2.开始
入门嘛,我们就找一个最简单的Login就可以了
新建Tomcat project,如下图
加入struts支持
新建一个FormBean:LoginForm
/**
*
*/
package com.test;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
/**
* @author lzy
*
*/
public class LoginForm extends ActionForm {
private String name=null;
private String password=null;
/* (non-Javadoc)
* @see org.apache.struts.action.ActionForm#reset(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
*/
@Override
public void reset(ActionMapping arg0, HttpServletRequest arg1) {
// TODO Auto-generated method stub
name=null;
password=null;
}
/* (non-Javadoc)
* @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
*/
@Override
public ActionErrors validate(ActionMapping arg0, HttpServletRequest arg1) {
// TODO Auto-generated method stub
ActionErrors errors=new ActionErrors();
if(name==null||name.length()<1){
errors.add("username",new ActionMessage("errors.login.name.empty"));
}
if(password==null||password.length()<1){
errors.add("password",new ActionMessage("errors.login.password.empty"));
}
return errors;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
/**
* @return Returns the password.
*/
public String getPassword() {
return password;
}
/**
* @param password The password to set.
*/
public void setPassword(String password) {
this.password = password;
}
}
新建一个Action:LoginAction
/**
*
*/
package com.test;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.*;
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 org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
/**
* @author lzy
*
*/
public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
LoginForm loginForm=(LoginForm)actionForm;
String name=loginForm.getName();
String password=loginForm.getPassword();
if(name.equals("name")&&password.equals("password")){
HttpSession session=request.getSession(true);
session.setAttribute("LOGGED_USER",name);
return mapping.findForward("success");
}
else{
ActionMessages errors=new ActionMessages();
errors.add("login.failure",new ActionMessage("errors.login.failure"));
this.saveErrors(request,errors);
return mapping.findForward("failure");
}
}
}
新建两个JSP页面,login.jsp,index.jsp
login.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-tiles" prefix="tiles" %>
<html:html>
<head>
<html:base />
</head>
<body>
<center>
<html:errors />
<html:form action="/login.do" method="POST">
<table border="1">
<tr>
<td colspan="2"><bean:message key="login.jsp.form.title"/></td>
</tr>
<tr>
<td align="right"><bean:message key="login.jsp.name"/></td>
<td><html:text property="name" size="20"></html:text></td>
</tr>
<tr>
<td align="right"><bean:message key="login.jsp.password"/></td>
<td><html:password property="password" size="20"></html:password></td>
</tr>
<tr>
<td colspan="2">
<html:submit><bean:message key="form.submit"/></html:submit>
<html:reset><bean:message key="form.reset"/></html:reset>
</td>
</tr>
</table>
</html:form>
</center>
</body>
</html:html>
index.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-nested" prefix="nested" %>
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
<title></title>
</head>
<body>
hello
<logic:present name="LOGGED_USER">,<bean:write name="LOGGED_USER"/>
</logic:present>
</body>
</html:html>
编辑WEB-INF/lib下的struts-config.xml
struts-config.xml
<?xml version="1.0"?>
<!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>
</data-sources>
<form-beans>
<form-bean name="loginForm" type="com.lzy.LoginForm"/>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
</global-forwards>
<action-mappings>
<action path="/login" name="loginForm" type="com.lzy.LoginAction" scope="session" validate="true" input="/login.jsp">
<forward name="failure" path="/login.jsp"/>
<forward name="success" path="/index.jsp"/>
</action>
</action-mappings>
<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<message-resources parameter="MessageResources"/>
</struts-config>
最后是资源文件
# -- standard errors --
errors.header=<UL><font color="red"><h2>Errors:</h2>
errors.prefix=<LI>
errors.suffix=</LI>
errors.footer=</font></UL><hr>
form.submit=Submit
form.reset=Reset
# -- login.jsp --
login.jsp.title=Login
login.jsp.form.title=Login
login.jsp.name=Name:
login.jsp.password=Password:
# -- login errors --
errors.login.name.empty=The name can not be empty!
errors.login.password.empty=The password can not be empty!
errors.login.failure=Login failed!<br> Please check your username and password!
3.测试
在test工程中选择tomcat project->Update context definition
然后运行Tomcat
http://127.0.0.1:8080/test/login.jsp