struts入门简单实例
环境:Ecplise3.2 + Tomcat5.5 + struts1.2.9
工程结构分布图:
web.xml文件内容:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<display-name>studyStruts</display-name>
<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>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
</web-app>
struts-config.xml文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="org.apache.my.form.LoginForm"/>
</form-beans>
<action-mappings>
<action
path="/login"
type="org.apache.my.action.LoginAction"
name="loginForm"
scope="request"
input="/login.jsp">
<forward name="success" path="/main.jsp"/>
<forward name="failed" path="/error.jsp"/>
</action>
</action-mappings>
<message-resources parameter="org.apache.my.ApplicationResources">
</message-resources>
</struts-config>
LoginAction.java文件内容:
package org.apache.my.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.my.form.LoginForm;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public final class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String userName = ((LoginForm) form).getUsername();
String password = ((LoginForm) form).getPassword();
if (userName.equals("suihu") && password.equals("sh")) {
HttpSession session = request.getSession();
session.setAttribute("loggedIn", "1");
return mapping.findForward("success");
}
else {
return mapping.findForward("failed");
}
}
}
LoginForm.java文件内容:
package org.apache.my.form;
import org.apache.struts.action.ActionForm;
public class LoginForm extends ActionForm {
public String username="";
public String password="";
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
login.jsp文件内容:
<%@ page contentType="text/html;charset=GBK"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html locale="true">
<head>
<title> Struts 主页 </title>
<html:base/>
</head>
<body>
<html:errors/>
<br>
<html:form action="/login" focus="username">
<table>
<tr>
<td>用户名: </td>
<td><html:text property="username"/></td>
</tr>
<tr>
<td>密码: </td>
<td><html:password property="password" redisplay="false"/></td>
</tr>
<tr>
<td colspan=2 align=right>
<html:submit>确定</html:submit>
</td>
</tr>
</table>
</html:form>
</body>
</html:html>
index.jsp文件内容:
index!!!
main.jsp文件内容:
success!!!
error.jsp文件内容:
failed!!!
实例已完成,启动你的应用服务器查看一下结果吧! |