zheyiw 发表于 2013-2-6 09:55:27

最简单的struts完整例子

一、安装MyEclipse(唯一要求,安置MyEclipse6.5.1以下版本,高版本没有测试过) 二、创建Web工程MyStruts 三、右键工程名,选择MyEclipse—>Add struts Cababilites 四、右键webRoot新建login.jsp,welcome.jsp和error.jsp。分别如下: 代码:login.jsp <%@ page language="java" contentType="text/html; charset=GBK"%> <html>   <head>      <title>登录页面</title>   </head>   <body>      <!-- 提交请求参数的表单 -->      <form action="login.do" name="login" method="post">            <table align="center">               <caption>                   用户登录               </caption>               <tr>                   <!-- 用户名的表单域 -->                   <td>                      用户名:                      <input type="text" name="username" />                   </td>               </tr>               <tr>                   <!-- 密码的表单域 -->                   <td>                      密  码:                      <input type="password" name="password" />                   </td>               </tr>               <tr align="center">                   <td colspan="2">                      <input type="submit" value="O K" />                      <input type="reset" value="重填" />                   </td>               </tr>            </table>      </form>   </body> </html> 代码:welcome.jsp <%@ page language="java" contentType="text/html; charset=GBK"%> <html>   <head>         <title>成功页面</title>   </head>   <body>      <h1> 您已经登录!</h1>   </body> </html> 代码:error.jsp <%@ page language="java" contentType="text/html; charset=GBK"%> <html>   <head>         <title>error!</title>   </head>   <body>         <h1>登陆失败!</h1>   </body> </html> 五、新建包lee 六、新建类lee.LoginAction.java,代码如下 代码:LoginAction.java package lee; 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 LoginAction extends Action {      // 处理用户请求的execute方法      public ActionForward execute(ActionMapping mapping, ActionForm actionForm,                      HttpServletRequest request, HttpServletResponse response) {               // 为tiger时,返回success               // 字符串               // 否则返回error字符串               String uname = request.getParameter("username");               String upassword = request.getParameter("password");               if (uname.equals("scott") && upassword.equals("tiger")) {                      return mapping.findForward("success");               } else {                      return mapping.findForward("error");               }      } } 七、在webRoot/WEB-INF目录下找到struts-config.xml,修改成下面的样子: 代码: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-beans>   <global-exceptions />   <global-forwards />   <action-mappings>          <action         path="/login"          name="login"          type="lee.LoginAction"          scope="request" >          <forward name="success" path="/welcome.jsp" />          <forward name="error" path="/error.jsp" />      </action>          </action-mappings>   <message-resources parameter="com.yourcompany.struts.ApplicationResources" /> </struts-config> 八、启动服务器。如下图,先选第一个图标部署,部署服务器选择MyEclipse Tomcat;在按第二个启动服务器。 九、服务器启动成功,在浏览器中输入http://localhost:8080/MyStruts/login.jsp 十、输入默认用户名“scott”和密码“tiger”测试。说明:MyEclipse自带的struts是1.2版本的,现在流行的是struts2.0;两个版本在配置和类的书写上有区别。想使用struts2.0的请自己学习。原理是一样的。
页: [1]
查看完整版本: 最简单的struts完整例子