首页 > 代码库 > 基于struts环境下的jquery easyui环境搭建

基于struts环境下的jquery easyui环境搭建

下载地址:

http://download.csdn.net/detail/cyberzhaohy/7348451

添加了json包:jackson-all-1.8.5.jar,项目结构如下:

测试网址:

http://localhost:9090/jquery-easyui-1.2.6BasedonStruts/layout.jsp

效果图如下:

配置文件web.xml:

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appversion="2.4"xmlns="http://java.sun.com/xml/ns/j2ee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <welcome-file-list>

       <welcome-file>index.jsp</welcome-file>

    </welcome-file-list>

    <listener>

       <listener-class>

           org.springframework.web.context.ContextLoaderListener

       </listener-class>

    </listener>

    <context-param>

       <param-name>contextConfigLocation</param-name>

       <param-value>classpath:beans.xml</param-value>

    </context-param>

 

    <filter>

       <filter-name>openSessionInView</filter-name>

       <filter-class>

           org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

       </filter-class>

       <init-param>

           <param-name>sessionFactoryBeanName</param-name>

           <param-value>sf</param-value>

       </init-param>

    </filter>

    <filter-mapping>

       <filter-name>openSessionInView</filter-name>

       <url-pattern>/*</url-pattern>

    </filter-mapping>

    <filter>

       <filter-name>struts2</filter-name>

       <filter-class>

           org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

       </filter-class>

    </filter>

    <filter-mapping>

       <filter-name>struts2</filter-name>

       <url-pattern>/*</url-pattern>

    </filter-mapping>

 

</web-app>

 

beans.xml配置,上面配置文件黄色代码sf使用下面黄色代码配置:

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

          http://www.springframework.org/schema/context

          http://www.springframework.org/schema/context/spring-context-2.5.xsd

          http://www.springframework.org/schema/aop

           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

          http://www.springframework.org/schema/tx

          http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <context:annotation-config/>

    <context:component-scanbase-package="com.xdy"></context:component-scan>

    <bean

       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

       <propertyname="locations">

           <value>classpath:jdbc.properties</value>

       </property>

    </bean>

    <beanid="dataSource"

       class="org.apache.commons.dbcp.BasicDataSource"

       destroy-method="close">

       <propertyname="driverClassName"

           value="${jdbc.driverClassName}"/>

       <propertyname="url"value="${jdbc.url}"/>

       <propertyname="username"value="${jdbc.username}"/>

       <propertyname="password"value="${jdbc.password}"/>

    </bean>

    <beanid="sf"

       class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

       <propertyname="dataSource"ref="dataSource"/>

       <propertyname="packagesToScan">

           <list>

              <value>com.xdy.registration.model</value>

           </list>

       </property>

       <propertyname="hibernateProperties">

           <props>

              <propkey="hibernate.dialect">

                  org.hibernate.dialect.OracleDialect

              </prop>

              <propkey="hibernate.show_sql">true</prop>

           </props>

       </property>

    </bean>

    <beanid="hibernateTemplate"

       class="org.springframework.orm.hibernate3.HibernateTemplate">

       <propertyname="sessionFactory"ref="sf"></property>

    </bean>

 

    <beanid="txManager"

       class="org.springframework.orm.hibernate3.HibernateTransactionManager">

       <propertyname="sessionFactory"ref="sf"></property>

    </bean>

    <aop:config>

       <aop:pointcutid="bussinessService"

           expression="execution(public* com.xdy.registration.service.*.*(..))"></aop:pointcut>

       <aop:advisorpointcut-ref="bussinessService"

           advice-ref="txAdvice"/>

    </aop:config>

    <tx:adviceid="txAdvice"transaction-manager="txManager">

       <tx:attributes>

           <tx:methodname="exists"read-only="true"/>

           <tx:methodname="add*"propagation="REQUIRED"/>

       </tx:attributes>

    </tx:advice>

 

 

</beans>

 

控制逻辑struts.xml配置:

<!DOCTYPEstruts PUBLIC "-//Apache Software Foundation//DTDStruts Configuration 2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

    <constantname="struts.i18n.encoding"value="GBK"/>

    <packagename="registration"extends="struts-default">

 

       <actionname="u"

           class="com.xdy.registration.action.UserAction">

           <resultname="success">/registerSuccess.jsp</result>

           <resultname="fail">/registerFail.jsp</result>

           <resultname="list">/UserList.jsp</result>

           <resultname="load">/User.jsp</result>

       </action>

    </package>

</struts>

UserAction.java代码如下:

package com.xdy.registration.action;

 

import java.io.IOException;

import java.io.StringWriter;

import java.util.List;

 

import org.apache.struts2.ServletActionContext;

import org.codehaus.jackson.JsonFactory;

import org.codehaus.jackson.JsonGenerator;

import org.codehaus.jackson.map.ObjectMapper;

 

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.ModelDriven;

import com.xdy.registration.model.MyUser;

import com.xdy.registration.service.UserManager;

import com.xdy.registration.vo.UserRegisterInfo;

 

//@Component("user")

//@Component("u")

//@Scope("prototype")

publicclass UserActionextends ActionSupportimplements ModelDriven {

 

    public Object getModel() {

       //TODO Auto-generatedmethod stub

       returninfo;

    }

 

    /*

     * private Stringusername;; private String password; private String

     * password2;

     *

     * public StringgetUsername() { return username; }

     *

     * public voidsetUsername(String username) { this.username = username; }

     *

     * public StringgetPassword() { return password; }

     *

     * public voidsetPassword(String password) { this.password = password; }

     *

     * public StringgetPassword2() { return password2; }

     *

     * public voidsetPassword2(String password2) { this.password2 = password2; }

     */

    private UserRegisterInfoinfo = new UserRegisterInfo();

    private UserManageruserManager;

    private List<MyUser>users;

    private MyUsermyUser;

 

    public MyUser getMyUser() {

       returnmyUser;

    }

 

    publicvoid setMyUser(MyUser myUser) {

       this.myUser = myUser;

    }

 

    public String list() {

       this.users =this.userManager.getUsers();

       return"list";

    }

 

    public List<MyUser> getUsers() {

       returnusers;

    }

 

    publicvoid setUsers(List<MyUser> users) {

       this.users = users;

    }

 

    public UserAction() {

       System.out.println("useractioncreated!");

    }

 

    /*

     * public UserAction() {ApplicationContext ctx = new

     *ClassPathXmlApplicationContext("beans.xml"); um = (UserManager)

     *ctx.getBean("userManager"); }

     */

    @Override

    public String execute()throws Exception {

       System.out.println(info.getUsername());

       /*

        * if (password !=password2) { return "fail"; }

        */

       MyUser u = new MyUser();

       u.setUsername(info.getUsername());//username);

       u.setPassword(info.getPassword());//password);

       if (userManager.exists(u)) {

           return"fail";

       }

       userManager.add(u);

       return"success";

    }

 

    publicvoid datagird() {

       list();

       writeJson(this.users);

    }

 

    publicvoid writeJson(Object o) {

       String json=getJsonString(o);

       try{

           ServletActionContext.getResponse().getWriter().write(json);

       }catch(IOException e){

           e.printStackTrace();

       }

      

    }

 

    public String getJsonString(Object o) {

       ObjectMapper om=new ObjectMapper();

       StringWriter sw=new StringWriter();

       try{

           JsonGenerator jg=new JsonFactory().createJsonGenerator(sw);

           om.writeValue(jg, o);

           jg.close();

       }

       catch(IOException e){

           e.printStackTrace();

       }

       return sw.toString();

    }

 

    public UserManager getUserManager() {

       returnuserManager;

    }

 

    publicvoid setUserManager(UserManager userManager) {

       this.userManager = userManager;

    }

 

    public UserRegisterInfo getInfo() {

       returninfo;

    }

 

    publicvoid setInfo(UserRegisterInfo info) {

       this.info = info;

    }

 

    public String load() {

       this.myUser =this.userManager.loadById(info.getId());

       return"load";

    }

}

界面调用layout.jsp代码:

<%@ pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>

<%

    String path = request.getContextPath();

    String basePath = request.getScheme() +"://"

           + request.getServerName() +":" + request.getServerPort()

           + path + "/";

%>

 

<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

    <head>

       <basehref="<%=basePath%>">

 

       <title>My JSP‘layout.jsp‘ starting page</title>

 

       <metahttp-equiv="pragma"content="no-cache">

       <metahttp-equiv="cache-control"content="no-cache">

       <metahttp-equiv="expires"content="0">

       <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

       <metahttp-equiv="description"content="This is my page">

       <!--

    <link rel="stylesheet" type="text/css"href=http://www.mamicode.com/"styles.css">

    -->

       <scripttype="text/javascript"src="js/jquery-1.7.2.min.js"charset="UTF-8"></script>

       <scripttype="text/javascript"src="js/jquery.easyui.min.js"charset="UTF-8"></script>

       <linkrel="stylesheet"href="js/themes/default/easyui.css"

           type="text/css"></link>

       <linkrel="stylesheet"href="js/themes/icon.css"type="text/css"></link>

       <scripttype="text/javascript"src="js/locale/easyui-lang-zh_CN.js"charset="UTF-8"></script>

       <scriptcharset="UTF-8">

       varcc;

       $(function(){

           cc = $(‘#cc‘).layout();

           cc.layout(‘collapse‘,‘west‘);

       });

       functiongetCenterPanel(){

           var centerPanel=$(‘#cc‘).layout(‘panel‘,‘center‘);

           console.info(centerPanel.panel(‘options‘).title);

       }

       </script>

    </head>

 

    <body id="cc">

       <divregion="north"title="NorthTitle"

           style="height:100px;">

           <inputtype="button"value="展开西部面板"onclick="cc.layout(‘expand‘,‘west‘);"/>

           </div>

       <divregion="south"title="southTitle"split="true"

           style="height:100px;">

           <inputtype="button"value="获得center面板"onclick="getCenterPanel();"/>

           </div>

       <divregion="west"title="westTitle"split="true"collapse="true"

           style="width:200px;"></div>

       <divregion="center"iconCls="icon-save"href="center.html"title="centerTitle"style="overflow:hidden;">

          

           </div>

    </body>

</html>

被引入代码center.html如下:

<scripttype="text/javascript"charset="utf-8">

    vardatagrid;

    $(function(){

       $(‘#datagrid‘).datagrid({

           //url:sy.bp()+‘/userAction!datagrid‘,

           url:‘u!datagird.action‘,

           title:‘ddddd‘,

           iconCls:‘icon-save‘,

           pagination:true,

           pageSize:10,

           pageList:[10,20,30,40],

           fit:true,

           fitColumns:false,

           nowarp:false,

           border:false,

           idField:‘id‘,

           columns:[[{

              title:编号,

              field:‘id‘,

              width:100

           },{

              title:姓名,

              field:‘name‘,

              width:100

           },{

              title:密码,

              field:‘password‘,

              width:100

           }]]

       });

    });

</script>

<div class="easyui-tabs"fit="true"border="false">

    <divtitle="user management管理"border="false">

    <tableid="datagrid"></table>

    </div>

</div>