myeclipse开发struts应用程序小示例

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

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

6-26-1.GIF


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

6-26-2.GIF


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

6-26-3.GIF


然后修改/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:
  

6-26-11.GIF

 完成后,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:

6-26-5.GIF
 
因为我们这只是简单的演示一个登录片段,所以不用验证用户信息是否合法,所以将 Option Details的method选项卡的新建方法去掉,如图:

6-26-6.GIF

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

6-26-7.GIF

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

6-26-8.GIF

6-26-9.GIF

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

6-26-10.GIF

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

修改LoginAction如下所示:

ExpandedBlockStart.gif ContractedBlock.gif /***/ /**
InBlock.gif*Methodexecute
InBlock.gif*
@parammapping
InBlock.gif*
@paramform
InBlock.gif*
@paramrequest
InBlock.gif*
@paramresponse
InBlock.gif*
@returnActionForward
ExpandedBlockEnd.gif
*/

None.gif
public ActionForwardexecute(
None.gifActionMappingmapping,
None.gifActionFormform,
None.gifHttpServletRequestrequest,
None.gifHttpServletResponseresponse)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifLoginFormloginForm
=(LoginForm)form;
InBlock.gif
//TODOAuto-generatedmethodstub
InBlock.gif

InBlock.gifStringname
=loginForm.getUserName();
InBlock.gifStringpassword
=loginForm.getPassword();
InBlock.gifDBbasemyDb
=newDBbase();
InBlock.gif
InBlock.gifResultSetrs
=null;
InBlock.gif
intresult=0;
InBlock.gif
InBlock.gifStringsql
="selectcount(*)ascountfromuserswhereusername='"+name+"'andpassword='"+password+"'";
InBlock.gif
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifrs
=myDb.executeQuery(sql);
InBlock.gif
if(rs.next())
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifresult
=rs.getInt("count");
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
catch(SQLExceptionex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifex.printStackTrace();
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
if(result>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifrequest.setAttribute(
"userName",name);
InBlock.gif
returnmapping.findForward("success");
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
returnmapping.findForward("failure");
ExpandedBlockEnd.gif}

加入一个数据访问javaBean:

None.gif package com.vitamin.DataAccess;
None.gif
None.gif
import java.sql. * ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public class DBbase dot.gif {
InBlock.gifStringsDBDriver
="sun.jdbc.odbc.JdbcOdbcDriver";
InBlock.gifStringsConnstr
="jdbc:odbc:myDB";
InBlock.gifConnectionconnect
=null;
InBlock.gifResultSetrs
=null;
InBlock.gifStatementstmt
=null;
InBlock.gif
InBlock.gif
InBlock.gif
publicDBbase()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gifClass.forName(sDBDriver);
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(ClassNotFoundExceptionex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.err.println(ex.getMessage());
InBlock.gif
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
publicResultSetexecuteQuery(Stringsql)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.connect=DriverManager.getConnection(sConnstr);
InBlock.gif
this.stmt=this.connect.createStatement();
InBlock.gifrs
=stmt.executeQuery(sql);
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(SQLExceptionex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.err.println(ex.getMessage());
ExpandedSubBlockEnd.gif}

InBlock.gif
returnrs;
ExpandedSubBlockEnd.gif}

InBlock.gif
publicintexecuteUpdate(Stringsql)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
intresult=0;
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.connect=DriverManager.getConnection(sConnstr);
InBlock.gif
this.stmt=this.connect.createStatement();
InBlock.gifresult
=stmt.executeUpdate(sql);
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(SQLExceptionex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.err.println(ex.getMessage());
ExpandedSubBlockEnd.gif}

InBlock.gif
returnresult;
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif


好了,完成!!用浏览器上打开: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:

None.gif package com.vitamin.util;
None.gif
None.gif
import javax.servlet.Filter;
None.gif
import javax.servlet.FilterConfig;
None.gif
import javax.servlet.FilterChain;
None.gif
import javax.servlet.ServletException;
None.gif
import javax.servlet.ServletRequest;
None.gif
import javax.servlet.ServletResponse;
None.gif
import java.io.IOException;
None.gif
None.gif
public class EncodingFilter implements Filter
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
privateFilterConfigconfig=null;
InBlock.gif
privateStringtargetEncoding="GBK";
InBlock.gif
publicEncodingFilter()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
super();
InBlock.gif
//TODO自动生成构造函数存根
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
publicvoidinit(FilterConfigconfig)throwsServletException
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//TODO自动生成方法存根
InBlock.gif
this.config=config;
InBlock.gif
this.targetEncoding=config.getInitParameter("encoding");//从配置中读取初始化参数
InBlock.gif

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)throwsIOException,ServletException
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//TODO自动生成方法存根
InBlock.gif
System.out.println("目标编码:"+this.targetEncoding);
InBlock.gifrequest.setCharacterEncoding(
this.targetEncoding);
InBlock.gifresponse.setCharacterEncoding(
this.targetEncoding);
InBlock.gifchain.doFilter(request,response);
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
publicvoiddestroy()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//TODO自动生成方法存根
InBlock.gif
this.config=null;
InBlock.gif
this.targetEncoding=null;
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif

这样就可以很好地解决中文乱码的问题了。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值