struts1使用的简单例子
struts1包下载地址:http://www.apache.org
第一步:建一个web工程struts1,然后解压下载到的struts压缩包-->,解压后的目录为
apps文件夹下都是一些struts1的例子,把struts-blank-1.3.10应用解压,然后把其lib目录下的jar文件拷贝到自己工程lib目录下,这些jar文件是:
第二步:在web.xml文件中添加配置,配置主要内容为:
- <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>
- <load-on-startup>2</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>action</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
第三步:把struts-blank-1.3.10应用下的struts-config.xml文件拷贝到自己工程WEB-INF目录下,编辑内容:
- <?xml version="1.0" encoding="ISO-8859-1" ?>
- <!DOCTYPE struts-config PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
- "http://struts.apache.org/dtds/struts-config_1_3.dtd">
- <struts-config>
- <form-beans>
- <span style="color:#ff6666;"><form-bean name="loginForm" type = "com.struts1.app.form.login.LoginForm"/></span>
- </form-beans>
- <global-exceptions>
- </global-exceptions>
- <global-forwards>
- <forward
- name="welcome"
- path="/Welcome.do"/>
- </global-forwards>
- <action-mappings>
- <action
- path="/Welcome"
- forward="/pages/Welcome.jsp"/>
- <action path = "/login" name = "loginForm" scope = "request"
- type = "com.struts1.app.action.login.LoginAction" validate = "false">
- <span style="color:#ff0000;"><forward name = "success" path = "/success.jsp"/>
- <forward name = "login" path = "/login.jsp"/></span>
- </action>
- </action-mappings>
- <message-resources parameter="MessageResources" />
- <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
- <set-property
- property="pathnames"
- value="/org/apache/struts/validator/validator-rules.xml,
- /WEB-INF/validation.xml"/>
- </plug-in>
- </struts-config>
第四步:编写LoginForm.java:
- /*
- * LoginForm.java
- */
- package com.struts1.app.form.login;
- import org.apache.struts.action.ActionForm;
- @SuppressWarnings("serial")
- public class LoginForm extends ActionForm{
- 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;
- }
- }
第五步:编写LoginAction.java:
- package com.struts1.app.action.login;
- 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.struts1.app.form.login.LoginForm;
- public class LoginAction extends Action{
- @Override
- public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
- // TODO Auto-generated method stub
- String str = "";
- LoginForm login = (LoginForm) form;
- if (login.getUsername().equals("") || login.getPassword().equals("") || login.getUsername() == null || login.getPassword() == null) {
- request.setAttribute("message", "用户名或密码不能为空!");
- str = "login";
- } else if (login.getUsername().equals("zhangsan") && login.getPassword().equals("123")) {
- str = "success";
- }
- return mapping.findForward(str);
- }
- }
第六步:编写jsp页面:
login.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
- <%
- 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%>">
- </head>
- <body>
- <span style="color:#ff0000;"> <form </span><span style="color:#3333ff;">action = "login.do"</span><span style="color:#ff0000;"> method = "post">
- 用户名:<input type = "text" name = "username" /><br/>
- 密 码:<input type = "password" name = "password"/></br>
- <input type = "submit" value = "登录"/>
- </form></span>
- <span style="color:#ff0000;"> <%
- String mess = (String)request.getAttribute("message");
- if(mess!=null){
- out.print("<script language = 'javascript'>alert('"+mess+"')</script>");
- }
- %></span>
- </body>
- </html>
success.jsp:
- <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
- <%
- 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%>">
- </head>
- <body>
- <span style="color:#ff0000;"> <script language = "javascript">
- alert("登陆成功!");
- </script></span>
- </body>
- </html>
第七步:
在地址栏输入http://localhost:8080/struts1/login.jsp运行
整个工程架构: