`
王者之剑
  • 浏览: 194410 次
  • 性别: Icon_minigender_1
  • 来自: 湖北
社区版块
存档分类
最新评论

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

    博客分类:
  • java
阅读更多

二、最简单的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对文章长度有限制,不过这样也方便些,整个贴出来确实太长了。我以后写的时候也要注意。

 

分享到:
评论
4 楼 王者之剑 2008-11-21  
frenzy917 写道

frenzy917 写道加到编译路径之后tomcat就运行不起来了...解决了,把struts.xml里面多余的空格都删了就好了谢谢~~

嗯,贴到网页中的代码经常会有这种问题,所以每章最后会给出一个打包的工程。
难得你还一步步照做,加油!
3 楼 frenzy917 2008-11-21  
frenzy917 写道

加到编译路径之后tomcat就运行不起来了...

解决了,把struts.xml里面多余的空格都删了就好了
谢谢~~
2 楼 王者之剑 2008-11-21  
frenzy917 写道

我完全是按照你说的这个做的,但是运行的时候说HTTP Status 404 - /bitrac/admin.jspx好像struts.xml没加载上这是为什么?src是作为普通包嘛?

src要加入到编译路径,这样编译的时候src下的xml文件会自动复制到
WEB-INF/classes下面
1 楼 frenzy917 2008-11-21  
我完全是按照你说的这个做的,但是运行的时候说HTTP Status 404 - /bitrac/admin.jspx

好像struts.xml没加载上
这是为什么?
src是作为普通包嘛?

相关推荐

Global site tag (gtag.js) - Google Analytics