栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

struts1入门

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

struts1入门

1. 在myEcliple中添加struts1.2支持:

项目上右键 - myEclipse - add struts Capabilities - 选择struts1.2 - ...

注:myEcliple会帮我们做几件事:1.导入struts1.2所需的包;2.在web.xml添加一个filter(org.apache.struts.action.ActionServlet);3.WEB-INF下添加几个tld文件(struts标签)和struts-config.xml、validator-rules.xml;4.src下新建了国际化文件ApplicationResources.properties。

2.通过struts-config.xml的图形化界面新建Form、Action、Jsp。

注:如果JavaBean自己编写,需继承ActionForm。页面通过xx.do可以进入对应action的execute方法。

3.如果action类继承DispatchAction,复制execute方法自定义方法名,如doLogin,在struts-config.xml中对该aciton的配置添加属性parameter="method",此时,就可以通过url参数method=doLogin进入doLogin方法。

4.错误处理机制:

javaBean中重写validate方法:

  ActionErrors errors = new ActionErrors();
  if(!this.name.equals("admin")){
   errors.add("loginError",new ActionMessage("name.is.not.exists"));
  }
  return errors;

ApplicationResources.properties中添加 name.is.not.exists=用户名不存在

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> //jsp页面引入struts1标签

//jsp页面显示错误信息

或在action方法中:

  LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
  ActionMessages messages = new ActionMessages();
  if(loginForm.getName().equals("admin")){
   messages.add("loginError",new ActionMessage("admin.can.not.login"));
  }
  if(!messages.isEmpty()){
   super.saveErrors(request.getSession(), messages);
   return mapping.findForward("input");
  }
  return mapping.findForward("login_success");

5.所生成的struts-config.xml:




  
  
  
    
     
    
  
  
  
  
  
  
           attribute="loginForm"
      input="/index.jsp"
      name="loginForm"
      path="/login"
      scope="request"
      type="com.yourcompany.struts.action.LoginAction">
      
      
    
  
  

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/343599.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号