struts2开发简单实例

struts2开发简单实例 

1.引入struts2相应的jar包,可以到网上下载,大把大把的。 

2.配置struts.xml 要放在src目录下 如下: 
Java代码   收藏代码
  1.    
  2.   
  3. <?xml version="1.0" encoding="UTF-8" ?>  
  4. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
  5. <struts>  
  6.     <!-- Struts 2 的Action必须放在包空间下 -->  
  7.     <package name="strutsqs" extends="struts-default">  
  8.         <!-- 定义action的名字以及action的实现类 -->  
  9.         <action name="LoginAciton" class="com.gjy.struts2.loginAction.LoginAction">  
  10.             <!-- 定义action的处理结果result,result有两个属性,其中name指定返回名称,tyle指定返回的类型 -->  
  11.             <!-- 处理Login错误时返回/error.jsp -->  
  12.                         <result name="error">/error.jsp</result>  
  13.             <!-- 处理Lognin正确时/suc.jsp -->  
  14.                     <result name="suc">/suc.jsp</result>  
  15.         </action>  
  16.     </package>      
  17. </struts>   

3.配置web.xml struts2与struts1不同,struts2是通过一系列的过滤器来组成过滤链来对用户的请求作处理 
如下: 
Java代码   收藏代码
  1.    
  2.   
  3. <?xml version="1.0" encoding="UTF-8"?>  
  4. <web-app version="2.5"   
  5.     xmlns="http://java.sun.com/xml/ns/javaee"   
  6.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  7.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  8.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  9.   <welcome-file-list>  
  10.     <welcome-file>index.jsp</welcome-file>  
  11.   </welcome-file-list>  
  12.   <filter>  
  13.     <filter-name>struts2</filter-name>  
  14.     <!--定义struts2的核心Filter的实现类 -->  
  15.     <filter-class>  
  16.         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
  17.     </filter-class>  
  18.   </filter>  
  19.   <filter-mapping>  
  20.     <filter-name>struts2</filter-name>  
  21.     <url-pattern>/*</url-pattern>  
  22.   </filter-mapping></web-app>  

3.写Action,struts2的acction不继承其它类,是一个普通的java类,它不与serverlet API 相偶合,降低了代码的可维护性,使得代码 
更加容易测试。 
如下: 

(1).自定义一个action接口 
Java代码   收藏代码
  1. package com.gjy.struts2.interfaceAction;  
  2.   
  3. public interface Action {  
  4.     public static String SUCCESS="suc";  
  5.     public static String ERROR="error";  
  6.       
  7.     public String execute()throws Exception;  
  8. }  

(2).写一个loginAction,实现action接口,此action的业务逻辑没有与LoginAction分开,可自行实现分开 
Java代码   收藏代码
  1. package com.gjy.struts2.loginAction;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import com.gjy.struts2.entity.Book;  
  8. import com.gjy.struts2.interfaceAction.Action;  
  9. import com.opensymphony.xwork2.ActionContext;  
  10.   
  11. public class LoginAction implements Action {  
  12.       
  13.     private String userName;  
  14.     private String passWord;  
  15.       
  16.     public String getUserName() {  
  17.         return userName;  
  18.     }  
  19.       
  20.     public void setUserName(String userName) {  
  21.         this.userName = userName;  
  22.     }  
  23.     public String getPassWord() {  
  24.         return passWord;  
  25.     }  
  26.     public void setPassWord(String passWord) {  
  27.         this.passWord = passWord;  
  28.     }  
  29.       
  30.       
  31.     //处理用护请求的execute方法  
  32.     public String execute() throws Exception  
  33.     {  
  34.       
  35.     if (getUserName().equals("gongjiayun")  
  36.     && getPassWord().equals("hehe") )  
  37.     {  
  38.         //通过ActionContext访问Web对象的Session对象,此处的ActionContext返回的是一个Map  
  39.         //虽然struts2的action中没有HttpSession对象,但我们也可以通过通过ActionContext访问Web对象的Session对象  
  40.         Map sessionMap = ActionContext.getContext().getSession();  
  41.         sessionMap.put("user" , getUserName());  
  42.         Book book = new Book();  
  43.         Object books[]= book.getBook();  
  44.         List<Object[]> bookList = new ArrayList<Object[]>();  
  45.         bookList.add(books);  
  46.         sessionMap.put("book", bookList);  
  47.         return SUCCESS;  
  48.     }  
  49.     else  
  50.     {  
  51.         return ERROR;  
  52.     }  
  53.     }  
  54. }  


(3).写一个实体类Book 
Java代码   收藏代码
  1. package com.gjy.struts2.entity;  
  2.   
  3. public class Book {  
  4.     private static Object book[]=new Object[4];  
  5.       
  6.     public Book(){  
  7.         book[0] = "《java编程思想》";  
  8.         book[1] = "《Struts2权威指南》";  
  9.         book[2] = "《精通Oracle》";  
  10.         book[3] = "《我的Flex我精通》";  
  11.     }  
  12.       
  13.     public Object[] getBook(){  
  14.         return book;  
  15.     }  
  16. }  

(4).写一个登陆页面login.jsp 
Java代码   收藏代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'login.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!--  
  20.     <link rel="stylesheet" type="text/css" href="styles.css">  
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.     <p>用户登陆</p>  
  27.     <form action="LoginAciton.action">  
  28.         username:<input type="text" id="userName" name="userName"/><br/><br/>  
  29.         password:<input type="password" id="passWord" name="PassWord"/><br/><br/>  
  30.         <input type="submit" value="submit">  
  31.     </form>  
  32.   </body>  
  33. </html>  

(5).写一个显示页面suc.jsp 
Java代码   收藏代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>  
  3. <%@ taglib prefix="s" uri="/struts-tags"%>  
  4. <%  
  5. String path = request.getContextPath();  
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  7. %>  
  8.   
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10. <html>  
  11.   <head>  
  12.     <base href="<%=basePath%>">  
  13.       
  14.     <title>My JSP 'suc.jsp' starting page</title>  
  15.       
  16.     <meta http-equiv="pragma" content="no-cache">  
  17.     <meta http-equiv="cache-control" content="no-cache">  
  18.     <meta http-equiv="expires" content="0">      
  19.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  20.     <meta http-equiv="description" content="This is my page">  
  21.     <!--  
  22.     <link rel="stylesheet" type="text/css" href="styles.css">  
  23.     -->  
  24.   </head>  
  25.     
  26.   <body>  
  27.       
  28.     <center><div>  
  29.     WelCome,${sessionScope.user},you can do anything! Long Long ago,I have a dream !<br>  
  30.     <table border="1px" bordercolor="blue" id="tbColor">  
  31.         <caption>作者gjy的图书</caption>  
  32.         <c:forEach var="books" items="${book}" >  
  33.         <tr >  
  34.             <td>书名:</td>  
  35.             <td>${books[0] }</td>  
  36.         </tr>  
  37.        <tr>  
  38.             <td>书名:</td>  
  39.             <td>${books[1] }</td>  
  40.         </tr>  
  41.         <tr>  
  42.             <td>书名:</td>  
  43.             <td>${books[2] }</td>  
  44.         </tr>  
  45.         <tr>  
  46.             <td>书名:</td>  
  47.             <td>${books[3] }</td>  
  48.         </tr>  
  49.         </c:forEach>  
  50.     </table>  
  51.     </div></center>  
  52.   </body>  
  53. </html>  

(6).写一个error.jsp 
Java代码   收藏代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'suc.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!--  
  20.     <link rel="stylesheet" type="text/css" href="styles.css">  
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.     error,you can do nothing! <br>  
  27.   </body>  
  28. </html>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值