一直是做ETL的,还颇有点心得,然后这个项目客户想要加个WEB的查询系统,
沟通过后,原来只是要一张表的查询,so easy,只是项目组4个人都对java无解.
老大看项目组最近比较轻闲,遂扔给我一本struts 2的书:交给你了...我昏.打开书照着第一章开始搭平台,下插件,编编举世闻名的Hello World,
我以前也做过.NET的WBB,不过这些经验对java无用,感觉两种平台的差异相距甚远.其实需求相当简单,输入查询条件,点按钮,出结果.
最开始的疑问是:点了查询按钮后,结果数据是在同一个页面出来,还是跳转,或者是重新开个新页面?
在struts.xml中配好action的result后发现是用的跳转.
然后我在默认的index.jsp上写好s:textfield,s:select....s:submit
写一个queryAction,写一个show.jsp,点按钮,数据出来了,心情不错.
再在show.jsp上加一个返回到index.jsp的链接,方便用户的下一次查询.接着开始完善,发现"机构"这个条件是要从数据库读出来供用户选择的,那么index.jsp就不能做为首页了,
写了个initAction,跳转到index.jsp,OK,机构数据出来了,用户可以选了."日期"这个字段必须要填,加验证吧,validation框架,貌似很强大...
但是在配struts.xml的INPUT reslut时,晕了,按说是应该回到index.jsp,不过因为要读取机构信息,
直接跳到index肯定报错,跳到initAction吧,也报错,可能是result里一定要配实际的页面吧(猜的),
只好又加了个default.html,这个页面什么都不做,直接跳转到initAction,struts.xml终于配好了.测试validation时,起作用了,日期为空时不查询了,但是也不报错,就好像刷新了一下.
估计是在queryAction->default.html->initAction->index.jsp的跳转中,验证的信息早丢了.烦.不知道是我笨还是咋的,问问大家,一般WEB的查询都怎么写:
1.结果数据是在同一个页面出来,还是跳转,或者是重新开个新页面?
2.像我这种查询条件需要读数据库的应该怎么做?我觉得我的做法笨,跳来跳去我都想哭了.
3.有没有简单的例子可供参考,书上都是用login做例子,可怜我连login都不需要.

解决方案 »

  1.   

    验证框架确实很麻烦~其实我喜好自己手写~看你怎么用了关于STRUTS~项目中也只是用了主要地核心地那部分而已
      

  2.   

    1 数据是放在一个页面 还是跳转 那就看客户需要怎样了
    2 可以用ajax
    3 你说的是struts2的例子么?
    数据库 建个表 弄几个简单的字段 然后加上hibernate 并从数据库映射出对应的PO
    然后写个DAO
    里面的方法类似下面的这个
    public List<StudentAction> find(){
    Session session = HibernateSessionFactory.getSession();
    Transaction tx = session.beginTransaction();
    List list = new ArrayList();
    try {
    tx.begin();
    list = session.createQuery("from Student").list();
    tx.commit();
    return list;
    } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();

    }finally{
    session.close();
    }
    return null;
    }
    好了 现在在来写action
    public class StudentAction {
    private HttpServletRequest request;
    private HttpServletResponse response;
    private Integer id;
    private String dece;
    private String love;
    private String username;
    private String lastmodifytime;
    private List resultList;//这个用户前台页面循环 显示数据库里映射出的这几个字段
    private StudentAction sa;//用户修改时 记录修改之前的值
    省略set get 方法
              public String execute(){
    Dao dao = new Dao();
    Student st = new Student();
    st.setDece(sa.getDece());
    st.setLove(sa.getLove());
    st.setUsername(sa.getUsername());
    st.setLastmodifytime(DataUtil.StringToTimestamp(sa.getUsername()));
    dao.insert(st);
    return "suc";
    }

    public String find(){
    ActionContext ctx = ActionContext.getContext();

    Dao dao = new Dao();
    try {
    resultList = dao.find();

    return "suc";
    } catch (Exception e) {
    // TODO: handle exception
    return "error";
    }
    }

    public String delete(){
    Dao dao = new Dao();
    HttpServletRequest request = ServletActionContext.getRequest(); 
    ActionContext ctx = ActionContext.getContext();
    Map m = ctx.getParameters();
    String id = request.getParameter("id");
    int ID = 0;
    ID = Integer.valueOf(id);
    dao.delete(ID);
    return "suc";
    }

    public String edit(){
    Dao dao = new Dao();
    HttpServletRequest request = ServletActionContext.getRequest(); 
    ActionContext ctx = ActionContext.getContext();
    Map m = ctx.getParameters();
    String id = request.getParameter("id");
    int ID = 0;
    ID = Integer.valueOf(id);
    sa = dao.edit(ID);
    return "suc";
    }

    public String update(){
    Dao dao = new Dao();
    Student st = new Student();
    HttpServletRequest request = ServletActionContext.getRequest(); 
    ActionContext ctx = ActionContext.getContext();
    Map m = ctx.getParameters();
    String ms = (String)m.get("saveStudent_sa_id");
    int ID = 0;
    st.setDece(sa.getDece());
    st.setId(sa.getId());
    st.setLove(sa.getLove());
    st.setUsername(sa.getUsername());
    dao.saveOrUpdate(st);
    return "suc";
    }
      }
    前台页面 比如显示数据库里的值
    <s:iterator value="resultList" status="index" id="l">
    <s:if test="${id == '18'}">
    test Success
    </s:if>
    <s:if test="#index.odd == true">
    <tr style="background-color:#FFFF00">
    </s:if>
    <s:else>
    <tr>
    </s:else>
    <td>用户名</td>
    <td>
    <s:property value="username"/>
    <s:property  value="love"/>
    <s:property value="lastmodifytime"/>
    <a href="Delete.action?id=<s:property id="l" value="id"/>">
    删除
    </a>
    <a href="Edit.action?id=<s:property id="l" value="id"/>">
    编辑
    </a>
    </td>
    </s:iterator>
      

  3.   

    struts.xml
    <?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.action.extension" value="action"/>
    <constant name="struts.i18n.encoding" value="gbk"></constant>
    <package name="mldn" extends="struts-default">
    <!-- 保存数据进入student -->
    <action name="saveStudent" class="org.pany.struts2.action.StudentAction">
    <result name="suc" type="redirect">/Find.faces</result>
    </action>
    <!-- 查询出所有student -->
    <action name="Find" class="org.pany.struts2.action.StudentAction" method="find">
    <result name="suc">/list.jsp</result>
    </action>
    <action name="Delete" class="org.pany.struts2.action.StudentAction" method="delete">
    <result name="suc" type="redirect">/Find.faces</result>
    </action>
    <action name="Edit" class="org.pany.struts2.action.StudentAction" method="edit">
    <result name="suc" >/insert.jsp</result>
    </action>
    <action name="Update" class="org.pany.struts2.action.StudentAction" method="update">
    <result name="suc" type="redirect">/Find.faces</result>
    </action>
    </package>
    </struts>
      

  4.   

    <result name="suc" type="redirect">/Find.faces </result> 改为
    <result name="suc" type="redirect">/Find.action </result>
      

  5.   

    用ajax,就不用跳转了,后台随你用C#、servlet还是啥啥啥框架
      

  6.   

    o(∩_∩)o...,慢慢来不急,你还没有懂得JAVA,照书上来总有错误的,等你会了就简单了
      

  7.   

    客户触发 查询操作(javascript--onclick)
    请求服务器处理(数据库操作,这地方可以使用一个bean来实现)
    返回结果(数据库返回结果集,保存到list)
    显示给客户(jsp页面显示list)
      

  8.   

    加油落,刚开始我也是非常郁闷啊,但是什么事都有个过程,只有在这不断的解决问题当中才能提高能力,用DEBUG调式一下
      

  9.   

    数据结果看你跳不跳页面啊,我觉得用Ajax比较好!用户体验好。
    同情,加油!O(∩_∩)O~
      

  10.   

    查询就楼主那么写,验证用js吧,我觉得好一些,要不你传了一堆值到后台,结果不好使,再回来,效率有点低result可以返回action,给个例子<result name="error" type="chain">initAction</result>
      

  11.   

    form验证要覆盖form的validate方法
    页面用到html:errors
    将错误信息先写入资源文
    ---web验证项目中我还没碰到过用struts验证框架的我做过的都是js验证
    个人感觉 -----验证我想jsp里js应该最好吧毕竟struts验证需要各个配置还要读资源文件麻烦,速度估计也没JS快
    同是新人--希望共同进步
      

  12.   

    先调用action查出数据再跳转到页面。
    另外,struts2的验证框架很烂,最好别用,结合js和ajax做页面的校验。
      

  13.   

    jf.
    很简单的,如果只是个查询的话根本不需要struts2,
    直接jsp就好了.
    最多写个服务层查询下数据库
      

  14.   

    LZ不错,直接struts 2了。
    可怜小弟还在JSP/servlet瞎折腾
      

  15.   

    项目小的话,没有必要用struts等框架,
    JDBC + servlet 就可以了
      

  16.   

    举世闻名的Hello World!
    顶啊
      

  17.   

    I can understand your mood when meeting technical issues. But don't give up. Everyone has to undergo this phase to grow up.
      

  18.   

    我更郁闷,我做毕业设计要用到excel 2007的提取和存储,导师让我自己研究,可是该从哪下手都不知道,只能一个人在那憋着,你说的那个struts我听都没听过,我也不知道该看什么书
      

  19.   

    最好写上namespace,要不错误多多,基本上都是些无法访问的问题
      

  20.   

    我跟楼主差不多,做过.NET
    输入页面,到action再回来到输入页面都整不明白!
    还有一些标签状态保存的问题,也是struts2
      

  21.   

    深表同情。LZ已经很强大了。建议楼主验证方面的工作还是交给JS吧。struts的验证框架很麻烦
      

  22.   

    始尽的泄分吧
    把分斗泄在我身上
    不要因为我是矫花而怜惜我不喜欢action跳就要学AJAX了
      

  23.   

    用浮动框架<iframe ...>就不用跳来跳去了
      

  24.   

    good good study,day day up!
      

  25.   

    我建议你用Eclipse开发,那上面是有集成的Struts架构的,用起来会很容易的。