二、最简单的SSH程序
概要
本文将完成最常见的login功能。
1.最简单的Struts程序
2.集成Spring和Hibernate
3.数据访问层采用JPA
1.最简单的Struts程序
说它是Struts程序是因为没有Struts运行不了,说它最简单是因为仅仅执行了一个转发。
1)配置Struts
1.1)在web.xml文件中加入以下配置:
- < filter >
- < filter-name > Struts2 </ filter-name >
- < filter-class >
- org.apache.struts2.dispatcher.FilterDispatcher
- </ filter-class >
- </ filter >
- < filter-mapping >
- < filter-name > Struts2 </ filter-name >
- < url-pattern > /* </ url-pattern >
- </ filter-mapping >
添加后的完整web.xml如下:
- <? xml version = " 1.0 " encoding = " UTF-8 " ?>
- < 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 " >
- < display-name > Bitrac Blog </ display-name >
- < filter >
- < filter-name > Struts2 </ filter-name >
- < filter-class >
- org.apache.struts2.dispatcher.FilterDispatcher
- </ filter-class >
- </ filter >
- < filter-mapping >
- < filter-name > Struts2 </ filter-name >
- < url-pattern > /* </ url-pattern >
- </ filter-mapping >
- < welcome-file-list >
- < welcome-file > index.jsp </ welcome-file >
- </ welcome-file-list >
- </ web-app >
1.2)在Bitrac\src下新建struts.xml文件(注:不带盘符的指在Eclipse中操作,以后不再说明),内容如下:
- <? xml version = " 1.0 " encoding = " UTF-8 " ?>
- < ! DOCTYPE struts PUBLIC
- " -//Apache Software Foundation//DTD Struts Configuration 2.0//EN "
- " http://struts.apache.org/dtds/struts-2.0.dtd " >
- < struts >
- < constant name = " struts.devMode " value = " true " />
- < constant name = " struts.configuration.xml.reload " value = " true " />
- < constant name = " struts.action.extension " value = " jspx " />
- < package name = " default " extends = " struts-default " >
- < action name = " admin " >
- < result > /admin/index.jsp </ result >
- </ action >
- </ package >
- </ struts >
2)新建/admin/index.jsp文件 (这样的形式指在Bitrac\WebRoot新建目录admin和文件index.jsp)内容如下:
- <%@ page contentType="text/html; charset=UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title> Struts Test</title>
- </head>
- <body>
- <p>Hello From Struts</p>
- </body>
- </html>
修改/index.jsp,内容如下:
- <%@ page contentType="text/html; charset=UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="refresh" content="0;url=admin.jspx" />
- </head>
- <body>
- <p>Loading ...</p>
- </body>
- </html>
3) 运行
点击eclipse工具条上的按钮启动Tomcat,
浏览http://localhost:8080/bitrac/,正常的话应该会看到“Hello From Struts”。
注:javaeye对文章长度有限制,不过这样也方便些,整个贴出来确实太长了。我以后写的时候也要注意。