`
sky_yangwang
  • 浏览: 51521 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

strus2应用

阅读更多

 

 

Struts2大致原理:

页面---动作---struts.xml---动作处理类---跳转页面;

表单,Action,配置文件;

实体类传递页面数据(ognl)

Validate()简单验证;

动作处理类,结果,跳转页面;

 

Struts2的其它配置文件中:

<package name="loginOpra" namespace="/WebsiteBackstage" extends="struts-default">

Namespace struts2命名空间,默认为当前空间

最好用模块命名,

http://localhost:8080/sshWeb/WebsiteBackstage/login.jsp 

命名为/WebsiteBackstage

 

Action实现类:

使用继承ActionSupport的写法;

 

Action的动态调用方法

Action执行的时候并不一定要执行execute方法,我们可以指定Action执行哪个方法:

通过methed属性指定执行方法:

可以在配置文件中配置Action的时候用method=来指定执行哪个方法

<action name="userAdd" class="com.bjsxt.struts2.user.action.UserAction" method="add">

            <result>/user_add_success.jsp</result>

</action>

    这样,只要在action的对象中有一个add的方法,并且返回类型为String就可以了。如果没有method属性,则默认执行execute()方法。

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

    public String add() {

       return SUCCESS;

    }  

}

 

登录验证提示:

动作实现类方法中:

this.addFieldError("userError""用户名或密码错误,请检查!");

 

<s:fielderror fieldName="userError" theme="simple"/> 

<s:fielderror>标签:获取使用addFieldError()方法添加的信息。

                   FiledName:指定信息的名称。

                   Theme: 指定显示的主题。

 注:使用此标签获取的错误信息,Struts强制添加了css的修饰。生成的HTML代码如下(不长用)

<ul class="errorMessage">

               <li><span>name is error</span></li>

               <li><span>name is too long</span></li>

     </ul>

Class=”errorMessage”是Struts2已经设置好的一个css了。这个方式不方便我们自定义样式。

另,非空的验证:

两种方式,上面的一种,validate的一种,

非空,错误验证,都要搭配action配置文件中的

<result name="input">/WebsiteBackstage/login.jsp</result>

 

列表页面数据绑定;

类:

List<Admin> list =  adminDao.getAll();

ActionContext.getContext().put("admins", list);

页面:

<s:iterator value="admins" status="itStatus">

   <li>

   <s:property value="#itStatus.count" />

   <s:property value="name"/>

   <s:property value="pwd"/>

   </li>

</s:iterator>

 

重定向错误:---1个action有重定向错误,分成两个就好了。

<action name="admin_*" class="service.AdminService"  method="{1}">

    <result name="success" type="redirect">admin_list2.action</result>

    <result name="update" >/WebsiteBackstage/sysMana/admin_update.jsp</result>

    <result name="input">/WebsiteBackstage/sysMana/admin_add.jsp</result>

   </action>

   <action name="admin_list2" class="service.AdminService" method="list">

<result name="success">/WebsiteBackstage/sysMana/admin_list.jsp</result>

</action>

 

动作带id的简单写法:

<a href="admin_delete?id=<s:property value='id'/>">删除</a> 

复杂的:

 <s:url action="admin_delete" id="myUrl">

       <s:param name="id" value="id"/>

    </s:url>

        <a href='<s:property value="#myUrl" />'>删除</a>

 

页面路径

使用绝对路径;

<%

String path = request.getContextPath();  //项目文件名;

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   //http路径;

%>

---获取到http://localhost:8080/sshWeb/到这里的路径; 

页面使用方式两种:

1),<a href="<%=basePath%>WebsiteBackstage/sysMana/admin_add.jsp">添加管理员</a> ---单个超链中补全使用。

2),head中加<base href="<%=basePath%>"> ,所有超链默认加载前缀;

 

<s:date format="yyyy-MM-dd"    name="date" />

 

全选删除

页面checkbox

<s:form id="adminForm" action="admin_deleteMore"  method="post" enctype="multipart/form-data">  

         全选:<input type="checkbox" name="allselect" id="allselect" onclick="check()"/>

         <input type="submit" name="submit" value="批量删除" onclick="return confirm('确定删除该?')?true:false;" />

    <s:iterator value="admins" status="itStatus">

   <li>

   <s:property value="#itStatus.count" />

   <s:property value="name"/>

   <s:property value="pwd"/>

   <s:url action="admin_goUpdate" id="myUrl">

       <s:param name="id" value="id"/>

    </s:url>

        <a href='<s:property value="#myUrl" />'>修改 </a>

           

        <a href="admin_delete?id=<s:property value='id'/>"   onclick="return confirm('确定删除?');">删除</a>

        <input type="checkbox" name="chkbox" id="chkbox" value='<s:property value="id"/>' />  

   </li>

</s:iterator>

</s:form> 

Js全选:

<script type="text/javascript">  

    function check() {  

        var confirmBox = document.getElementById("allselect");//  

        if (confirmBox.checked) {  

            var arryObj = document.getElementsByName("chkbox");  

            for ( var i = 0; i < arryObj.length; i++) {  

                if (typeof arryObj[i].type != "undefined"  

                        && arryObj[i].type == 'checkbox')  

                arryObj[i].checked = true

            }  

        } else {  

            var arryObj = document.getElementsByName("chkbox" 

            for ( var i = 0; i < arryObj.length; i++) {  

  

                if (typeof arryObj[i].type != "undefined"  

                        && arryObj[i].type == 'checkbox')  

                    arryObj[i].checked = false;  

            }  

        }  

    }

</script> 

实现类:

//多选删除 | 数组获取;循环删除

 public String deleteMore() throws Exception{  

 DaoDemoUtils.buildJndi();

 ApplicationContext ac = new ClassPathXmlApplicationContext("/dao/datasource-context-dao.xml");

 AdminDAO adminDao = (AdminDAO) ac.getBean("AdminDao");

 HttpServletRequest request=ServletActionContext.getRequest();  

 String[] ids = request.getParameterValues("chkbox");

 

 for(int i = 0; i < ids.length; i++)

 {

 Long id = Long.parseLong(ids[i].toString());

 adminDao.delete(id);

 }

 return "success";  

 }  

 

编辑器

见编辑器的实现文档;

 

修改页面编辑器不现实;

---路径不对,使用绝对路径。

 

jquery输入验证:

见jquery输入验证的文档。

 

 

添加

private DownloadFile downloadFile;

public DownloadFile getDownloadFile() {

return downloadFile;

}

public void setDownloadFile(DownloadFile downloadFile) {

this.downloadFile = downloadFile;

}

---其中,downloadFile必须是DownloadFile这个的首字母小写,否则出错。

 

 

上传下载

文件路径;

文件大小;

下载次数;

添加时间;

软件性质;状态; ---单选按钮组;

 

 

集合组件: ---给定集合选项。

1),最简单的静态写法:

添加页面,

<s:radio list="#{'0':'正常','1':'结束'}" name="downfile.state" value="0" label="状态"/>

修改页面,

<s:radio list="#{'0':'正常','1':'结束'}" name="downloadFile.state"  label="状态"/>    //struts2会自动绑定value对应的值;

2),简单动态的写法:

实体类;无数据表;

 

 

列表页面,显示值对应的汉字;

 

 

///////////////////////////////////////////////////////////////////////////////////////////////////

 

页面数据操作:

1,表单页面数据获取; ---登录页面,已解决。

添加; ---管理员添加;

列表页面数据绑定; ---管理员列表页面;

删除; ---管理员删除;

修改; ---管理员修改;

全选删除; ---管理员批量删除;

添加内容的编辑器; ---关于我们添加、修改页面;

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics