myeclipse开发struts应用程序小示例

今天看了下Struts框架的概述,就用myeclispe试着动手开发了一个简单的用户登录程序。应用程序包括2个jsp文件、一个ActionForm、一个Action以及其它。是:login.jsp(用户登录及错误提示页面),loginSuccess.jsp(提示登录成功页面),LoginForm.java(ActionForm,存放用户提交信息),LoginAction.java(Action,简单的处理用户登录事件)。

下面开始动手吧。。。
首先我们先建立一个j2ee的web project

myeclipse开发struts应用程序小示例


然后给这个项目添加Struts框架必要的文件.在我们项目名上点击右键,选择MyEclipes --> Add Struts Capabilities...弹出对话框图2:

myeclipse开发struts应用程序小示例


其中Struts config path就是我们的struts配置文件,URL pattern我们选择*.do,Default application resource为我们默认的资源文件地方,你可以选择它的存储位置,我们在这里保持默认。点击Finish后,项目结构类似于图3:

myeclipse开发struts应用程序小示例


然后修改/WEB-INF/web.xml文件,为其添加标签库。将下面代码添加至 </webapp> 上面:
<jsp-config>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
</jsp-config>
完成后,打开struts-config.xml文件,点击这个界面左下角的Design进入可视化设计界面。我们先建立loginSuccess.jsp文件.点击Palette面版上的创建JSP文件图标,弹出创建JSP文件面板。图4:
  

myeclipse开发struts应用程序小示例

 完成后,struts-config.xml文件自动被更新,可视化界在上也出现了刚新建的JSP模块。新建的jsp文件也打开了。覆盖所有的<%@ taglib ...... 为我们开始在/WEB-INF/web.xml中定义的:
  
  <%@ taglib uri="/tags/struts-html" prefix="html"%>
  <%@ taglib uri="/tags/struts-bean" prefix="bean"%>
  <%@ taglib uri="/tags/struts-logic" prefix="logic"%>
  
  然后在<body></body>中添加:
  Hello <bean:write name="userName" scope="request" /> .
  这里将request中的属性userName输出在页面上,所以等下我们在loginAction中,登录成功后要设置一个相关属性。

下面来开始我们最后三个文件的设计吧。在Struts-config.xml的Design模式中,在画版的空白区域点右键,选择New --> New Form, Action and JSP 弹出ActionForm的选项面板,我们按图上输入相关值,图5:

myeclipse开发struts应用程序小示例
 
因为我们这只是简单的演示一个登录片段,所以不用验证用户信息是否合法,所以将 Option Details的method选项卡的新建方法去掉,如图:

myeclipse开发struts应用程序小示例

接下来选择 Optional Details的JSP选项卡,我们选中Create JSP form? 这一步myeclipse将为我们创建一个简单的与用户交互的登录页面:

myeclipse开发struts应用程序小示例

点Next,进入Action选项面板.将Option Details的Form选项卡中Validate Form取消选择,如图:

myeclipse开发struts应用程序小示例

myeclipse开发struts应用程序小示例

点击Finish完成。在Struts-config.xml的Design中,可以看到图所示:

myeclipse开发struts应用程序小示例

  最后,简单的修改一下login.jsp,将所有<%@ taglib ...%>替换为:
  <%@ taglib uri="/tags/struts-html" prefix="html"%>
  <%@ taglib uri="/tags/struts-bean" prefix="bean"%>

修改LoginAction如下所示:

myeclipse开发struts应用程序小示例myeclipse开发struts应用程序小示例/***//**
myeclipse开发struts应用程序小示例*Methodexecute
myeclipse开发struts应用程序小示例*
@parammapping
myeclipse开发struts应用程序小示例*
@paramform
myeclipse开发struts应用程序小示例*
@paramrequest
myeclipse开发struts应用程序小示例*
@paramresponse
myeclipse开发struts应用程序小示例*
@returnActionForward
myeclipse开发struts应用程序小示例
*/

myeclipse开发struts应用程序小示例
publicActionForwardexecute(
myeclipse开发struts应用程序小示例ActionMappingmapping,
myeclipse开发struts应用程序小示例ActionFormform,
myeclipse开发struts应用程序小示例HttpServletRequestrequest,
myeclipse开发struts应用程序小示例HttpServletResponseresponse)
myeclipse开发struts应用程序小示例myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例{
myeclipse开发struts应用程序小示例LoginFormloginForm
=(LoginForm)form;
myeclipse开发struts应用程序小示例
//TODOAuto-generatedmethodstub
myeclipse开发struts应用程序小示例

myeclipse开发struts应用程序小示例Stringname
=loginForm.getUserName();
myeclipse开发struts应用程序小示例Stringpassword
=loginForm.getPassword();
myeclipse开发struts应用程序小示例DBbasemyDb
=newDBbase();
myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例ResultSetrs
=null;
myeclipse开发struts应用程序小示例
intresult=0;
myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例Stringsql
="selectcount(*)ascountfromuserswhereusername='"+name+"'andpassword='"+password+"'";
myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例
try
myeclipse开发struts应用程序小示例myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例{
myeclipse开发struts应用程序小示例rs
=myDb.executeQuery(sql);
myeclipse开发struts应用程序小示例
if(rs.next())
myeclipse开发struts应用程序小示例myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例{
myeclipse开发struts应用程序小示例result
=rs.getInt("count");
myeclipse开发struts应用程序小示例}

myeclipse开发struts应用程序小示例}

myeclipse开发struts应用程序小示例
catch(SQLExceptionex)
myeclipse开发struts应用程序小示例myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例{
myeclipse开发struts应用程序小示例ex.printStackTrace();
myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例}

myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例
if(result>0)
myeclipse开发struts应用程序小示例myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例{
myeclipse开发struts应用程序小示例request.setAttribute(
"userName",name);
myeclipse开发struts应用程序小示例
returnmapping.findForward("success");
myeclipse开发struts应用程序小示例}

myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例
returnmapping.findForward("failure");
myeclipse开发struts应用程序小示例}

加入一个数据访问javaBean:

myeclipse开发struts应用程序小示例packagecom.vitamin.DataAccess;
myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例
importjava.sql.*;
myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例myeclipse开发struts应用程序小示例
publicclassDBbasemyeclipse开发struts应用程序小示例{
myeclipse开发struts应用程序小示例StringsDBDriver
="sun.jdbc.odbc.JdbcOdbcDriver";
myeclipse开发struts应用程序小示例StringsConnstr
="jdbc:odbc:myDB";
myeclipse开发struts应用程序小示例Connectionconnect
=null;
myeclipse开发struts应用程序小示例ResultSetrs
=null;
myeclipse开发struts应用程序小示例Statementstmt
=null;
myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例
publicDBbase()
myeclipse开发struts应用程序小示例myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例{
myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例
try
myeclipse开发struts应用程序小示例myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例{
myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例Class.forName(sDBDriver);
myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例}

myeclipse开发struts应用程序小示例
catch(ClassNotFoundExceptionex)
myeclipse开发struts应用程序小示例myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例{
myeclipse开发struts应用程序小示例System.err.println(ex.getMessage());
myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例}

myeclipse开发struts应用程序小示例}

myeclipse开发struts应用程序小示例
publicResultSetexecuteQuery(Stringsql)
myeclipse开发struts应用程序小示例myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例{
myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例
try
myeclipse开发struts应用程序小示例myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例{
myeclipse开发struts应用程序小示例
this.connect=DriverManager.getConnection(sConnstr);
myeclipse开发struts应用程序小示例
this.stmt=this.connect.createStatement();
myeclipse开发struts应用程序小示例rs
=stmt.executeQuery(sql);
myeclipse开发struts应用程序小示例}

myeclipse开发struts应用程序小示例
catch(SQLExceptionex)
myeclipse开发struts应用程序小示例myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例{
myeclipse开发struts应用程序小示例System.err.println(ex.getMessage());
myeclipse开发struts应用程序小示例}

myeclipse开发struts应用程序小示例
returnrs;
myeclipse开发struts应用程序小示例}

myeclipse开发struts应用程序小示例
publicintexecuteUpdate(Stringsql)
myeclipse开发struts应用程序小示例myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例{
myeclipse开发struts应用程序小示例
intresult=0;
myeclipse开发struts应用程序小示例
try
myeclipse开发struts应用程序小示例myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例{
myeclipse开发struts应用程序小示例
this.connect=DriverManager.getConnection(sConnstr);
myeclipse开发struts应用程序小示例
this.stmt=this.connect.createStatement();
myeclipse开发struts应用程序小示例result
=stmt.executeUpdate(sql);
myeclipse开发struts应用程序小示例}

myeclipse开发struts应用程序小示例
catch(SQLExceptionex)
myeclipse开发struts应用程序小示例myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例{
myeclipse开发struts应用程序小示例System.err.println(ex.getMessage());
myeclipse开发struts应用程序小示例}

myeclipse开发struts应用程序小示例
returnresult;
myeclipse开发struts应用程序小示例}

myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例}

myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例


好了,完成!!用浏览器上打开:http://localhost:8080/HelloStruts/login.jsp,就可以了
哦,出了问题了,中文输入不支持,呵呵,想点办法来让个一劳永逸吧。。
Servlet2.3开始增加了事件监听和过滤器,管道和过滤器是为处理数据流的系统而提供的一种模式,它由管道和过滤器组成,每个处理步骤都封装在一个过滤器组件中,数据通过相邻过滤器之间的管道进行传输,每个过滤器可以单独修改,功能单一,并且顺序可以进行配置。
在Web.xml中修改如下:
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.vitamin.util.EncodingFilter</filter-class>

<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

新加一个类EncodingFilter:

myeclipse开发struts应用程序小示例packagecom.vitamin.util;
myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例
importjavax.servlet.Filter;
myeclipse开发struts应用程序小示例
importjavax.servlet.FilterConfig;
myeclipse开发struts应用程序小示例
importjavax.servlet.FilterChain;
myeclipse开发struts应用程序小示例
importjavax.servlet.ServletException;
myeclipse开发struts应用程序小示例
importjavax.servlet.ServletRequest;
myeclipse开发struts应用程序小示例
importjavax.servlet.ServletResponse;
myeclipse开发struts应用程序小示例
importjava.io.IOException;
myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例
publicclassEncodingFilterimplementsFilter
myeclipse开发struts应用程序小示例myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例{
myeclipse开发struts应用程序小示例
privateFilterConfigconfig=null;
myeclipse开发struts应用程序小示例
privateStringtargetEncoding="GBK";
myeclipse开发struts应用程序小示例
publicEncodingFilter()
myeclipse开发struts应用程序小示例myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例{
myeclipse开发struts应用程序小示例
super();
myeclipse开发struts应用程序小示例
//TODO自动生成构造函数存根
myeclipse开发struts应用程序小示例
}

myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例
publicvoidinit(FilterConfigconfig)throwsServletException
myeclipse开发struts应用程序小示例myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例{
myeclipse开发struts应用程序小示例
//TODO自动生成方法存根
myeclipse开发struts应用程序小示例
this.config=config;
myeclipse开发struts应用程序小示例
this.targetEncoding=config.getInitParameter("encoding");//从配置中读取初始化参数
myeclipse开发struts应用程序小示例

myeclipse开发struts应用程序小示例}

myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例
publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)throwsIOException,ServletException
myeclipse开发struts应用程序小示例myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例{
myeclipse开发struts应用程序小示例
//TODO自动生成方法存根
myeclipse开发struts应用程序小示例
System.out.println("目标编码:"+this.targetEncoding);
myeclipse开发struts应用程序小示例request.setCharacterEncoding(
this.targetEncoding);
myeclipse开发struts应用程序小示例response.setCharacterEncoding(
this.targetEncoding);
myeclipse开发struts应用程序小示例chain.doFilter(request,response);
myeclipse开发struts应用程序小示例}

myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例
publicvoiddestroy()
myeclipse开发struts应用程序小示例myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例{
myeclipse开发struts应用程序小示例
//TODO自动生成方法存根
myeclipse开发struts应用程序小示例
this.config=null;
myeclipse开发struts应用程序小示例
this.targetEncoding=null;
myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例}

myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例
myeclipse开发struts应用程序小示例}

myeclipse开发struts应用程序小示例

这样就可以很好地解决中文乱码的问题了。。。