平台环境:win7、MyEclipse 10
1 struts2 框架搭建
在MyEclipse新建web project 取名为Struts2_Test,复制jar包到webroot/web-inf/lib.整体的结构如下
注意:不同版本的struts 所必需的包不同,可根据报错加入缺少的包。
在project上右键->选择myeclipse->add struts capabilities
选择struts 2.1
点击next,这儿取消MyEclipse Libraries,因为我们已经把包放上去了。如果没有包也可以用MyEclipse自带的包。
点击finish.这时会在src下自动添加struts.xml文件。并且web.xml也会自动修改。
struts.xml如下:空的struts配置
1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> 4 5 <struts> 6 7 8 9 </struts>
web.xml 如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 4 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 7 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 8 9 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 10 11 <display-name></display-name> 12 13 <!-- 起始页面 --> 14 15 <welcome-file-list> 16 17 <welcome-file>index.jsp</welcome-file> 18 19 </welcome-file-list> 20 21 <!-- 过滤器 用于初始化struts2 --> 22 23 <filter> 24 25 <filter-name>struts2</filter-name> 26 27 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 28 29 </filter> 30 31 <!-- 用于struts2 的过滤器映射 --> 32 33 <filter-mapping> 34 35 <filter-name>struts2</filter-name> 36 37 <url-pattern>/*</url-pattern> 38 39 </filter-mapping> 40 41 </web-app>
部署project:点击工具栏上的部署按钮
选择要部署的project点击add
在Server下拉列表选择MyEclipse Tomcat
部署成功,无报错。选中project点击run
选择run as ->MyEclipse Server Application运行后结果如下
到此struts2 框架搭建成功。可以进行下一步:struts2 登录实例的实现。
2 struts2 登录实例的实现
- 在scr下创建名为org.struts.useraction的包(package) 再创建名为UserAction的类
UserAction.java的内容:
1 package org.struts.useraction; 2 3 4 5 import com.opensymphony.xwork2.ActionSupport; 6 7 8 9 public class UserAction extends ActionSupport { 10 11 12 13 private String username; 14 15 private String password; 16 17 18 19 public String getUsername() { 20 21 return username; 22 23 } 24 25 26 27 public void setUsername(String username) { 28 29 this.username = username; 30 31 } 32 33 34 35 public String getPassword() { 36 37 return password; 38 39 } 40 41 42 43 public void setPassword(String password) { 44 45 this.password = password; 46 47 } 48 49 50 51 public String execute() throws Exception { 52 53 System.out.println("Login.action"); 54 55 if ("scott".equals(username) && "tiger".equals(password))//scott是用户名,tiger是密码 56 57 return "success"; 58 59 else 60 61 return "error"; 62 63 } 64 65 }
- 然后在struts.xml配置login.action 代码如下:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> 4 5 <struts> 6 7 <!--struts2.0默认的配置文件 --> 8 9 <include file="struts-default.xml"></include> 10 11 <!-- 也可以加载其他的配置文件 --> 12 13 <!-- <include file="mystrutsconfig.xml"></include> --> 14 15 <!-- 添加package --> 16 17 <package name="useraction" extends="struts-default"> 18 19 <!-- 配置login.action --> 20 21 <action name="login" class="org.struts.useraction.UserAction"> 22 23 <result name="success">success.jsp</result> 24 25 <result name="error">error.jsp</result> 26 27 </action> 28 29 </package> 30 31 </struts>
- 修改index.jsp内容如下:
1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=GBK"%> 2 3 <% 4 5 String path = request.getContextPath(); 6 7 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 8 9 %> 10 11 <%@taglib uri="/struts-tags" prefix="s"%> 12 13 14 15 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 16 17 <html> 18 19 <head> 20 21 <base href="<%=basePath%>"> 22 23 24 25 <title>登陆界面</title> 26 27 <meta http-equiv="pragma" content="no-cache"> 28 29 <meta http-equiv="cache-control" content="no-cache"> 30 31 <meta http-equiv="expires" content="0"> 32 33 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 34 35 <meta http-equiv="description" content="This is my page"> 36 37 <!-- 38 39 <link rel="stylesheet" type="text/css" href="styles.css"> 40 41 --> 42 43 </head> 44 45 46 47 <body> 48 49 <!-- 提交请求参数的表单 --> 50 51 <form action="login" method="post"> 52 53 <table align="center"> 54 55 <caption> 56 57 <h3>用户登录</h3> 58 59 </caption> 60 61 <tr> 62 63 <!-- 用户名的表单域 --> 64 65 <td>用户名:<input type="text" name="username" /></td> 66 67 </tr> 68 69 <tr> 70 71 <!-- 密码的表单域 --> 72 73 <td>密 码:<input type="password" name="password" /></td> 74 75 </tr> 76 77 <tr align="center"> 78 79 <td colspan="2"><input type="submit" value="登录"/></td> 80 81 </tr> 82 83 </table> 84 85 </form> 86 87 </body> 88 89 </html>
- 在webroot下创建success.jsp 和error.jsp。 success.jsp 内容如下:
1 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> 2 3 <% 4 5 String path = request.getContextPath(); 6 7 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 8 9 %> 10 11 12 13 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 14 15 <html> 16 17 <head> 18 19 <base href="<%=basePath%>"> 20 21 22 23 <title>My JSP 'success.jsp' starting page</title> 24 25 26 27 </head> 28 29 30 31 <body> 32 33 This is success page. <br> 34 35 </body> 36 37 </html>
error.jsp 类似。
最后是部署运行,结果如下:
输入正确用户名(scott)和密码(tiger)登录成功!
输入错误的则会显示登录失败
这样登录实例就完成了~~~