Struts2+Spring2.5+Hibernate3.2实例教程(2-1)V0.1

二、最简单的SSH程序

概要
本文将完成最常见的login功能。
1.最简单的Struts程序
2.集成Spring和Hibernate
3.数据访问层采用JPA


1.最简单的Struts程序
说它是Struts程序是因为没有Struts运行不了,说它最简单是因为仅仅执行了一个转发。
1)配置Struts
1.1)在web.xml文件中加入以下配置:

  1. < filter >
  2.         < filter-name > Struts2 </ filter-name >
  3.         < filter-class >
  4.             org.apache.struts2.dispatcher.FilterDispatcher
  5.         </ filter-class >
  6.     </ filter >
  7.  
  8.     < filter-mapping >
  9.         < filter-name > Struts2 </ filter-name >
  10.         < url-pattern > /* </ url-pattern >
  11.   </ filter-mapping >


添加后的完整web.xml如下:

  1. <? xml version = " 1.0 " encoding = " UTF-8 " ?>
  2. < web-app   id = " bitrac " version = " 2.4 " xmlns = " http://java.sun.com/xml/ns/j2ee " xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation = " http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd " >
  3.     < display-name > Bitrac Blog </ display-name >
  4.    
  5.     < filter >
  6.         < filter-name > Struts2 </ filter-name >
  7.         < filter-class >
  8.             org.apache.struts2.dispatcher.FilterDispatcher
  9.         </ filter-class >
  10.     </ filter >
  11.  
  12.     < filter-mapping >
  13.         < filter-name > Struts2 </ filter-name >
  14.         < url-pattern > /* </ url-pattern >
  15.     </ filter-mapping >
  16.     
  17.     < welcome-file-list >
  18.         < welcome-file > index.jsp </ welcome-file >
  19.     </ welcome-file-list >
  20. </ web-app >


1.2)在Bitrac\src下新建struts.xml文件(注:不带盘符的指在Eclipse中操作,以后不再说明),内容如下:

  1. <? xml version = " 1.0 " encoding = " UTF-8 " ?>
  2. < ! DOCTYPE   struts PUBLIC
  3.     " -//Apache Software Foundation//DTD Struts Configuration 2.0//EN "
  4.     " http://struts.apache.org/dtds/struts-2.0.dtd " >
  5.    
  6. < struts >
  7.     < constant   name = " struts.devMode " value = " true " />
  8.     < constant   name = " struts.configuration.xml.reload " value = " true " />
  9.     < constant   name = " struts.action.extension " value = " jspx " />
  10.    
  11.     < package   name = " default " extends = " struts-default " >
  12.         < action   name = " admin " >
  13.             < result > /admin/index.jsp </ result >
  14.         </ action >      
  15.     </ package >   
  16. </ struts >


2)新建/admin/index.jsp文件 (这样的形式指在Bitrac\WebRoot新建目录admin和文件index.jsp)内容如下:

  1. <%@ page contentType="text/html; charset=UTF-8"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6.     <head>
  7.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  8.         <title> Struts Test</title>
  9.     </head>
  10.     <body>
  11.         <p>Hello From Struts</p>
  12.     </body>
  13. </html>


修改/index.jsp,内容如下:

  1. <%@ page contentType="text/html; charset=UTF-8"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head>
  7.     <meta http-equiv="refresh" content="0;url=admin.jspx" />
  8. </head>
  9. <body>
  10.     <p>Loading ...</p>
  11. </body>
  12. </html>


3)  运行
点击eclipse工具条上的按钮启动Tomcat,
浏览http://localhost:8080/bitrac/,正常的话应该会看到“Hello From Struts”。

 

注:javaeye对文章长度有限制,不过这样也方便些,整个贴出来确实太长了。我以后写的时候也要注意。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值