2012年6月29日

最近写了个项目,自己第一次搭建架构,费了好久才搭成功,记录下来,为以后留个参考。
前台使用的是jquery
web.xml 
 2   <context-param>
 3     <param-name>contextConfigLocation</param-name>
 4         <param-value>
 5           classpath*:applicationContext-common.xml, 
 6           classpath*:applicationContext-dao.xml,
 7           classpath*:applicationContext-service.xml,
 8           classpath*:applicationContext-action.xml
 9         </param-value>
10   </context-param>
11   
12   <welcome-file-list>
13     <welcome-file>login.jsp</welcome-file>
14   </welcome-file-list>
15   <filter>
16         <filter-name>struts2</filter-name>
17         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
18   </filter>
19   
20     <filter-mapping>
21         <filter-name>struts2</filter-name>
22         <url-pattern>/*</url-pattern>
23     </filter-mapping>
24         <listener>
25         <listener-class>
26             org.springframework.web.context.ContextLoaderListener 
27         </listener-class>
28     </listener>
29     <session-config> 
30         <session-timeout>20</session-timeout> 
31     </session-config>
32     <filter>
33        <filter-name>struts-cleanup</filter-name>
34        <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
35     </filter>
36     <filter-mapping>
37        <filter-name>struts-cleanup</filter-name>
38        <url-pattern>/*</url-pattern>
39     </filter-mapping>

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>
    <package name="default" namespace="/com" extends="struts-default">
       
        <action name="login" class="loginAction" method="login">
            <result name="success">/main.jsp</result>
            <result name="error">/loginError.jsp</result>
            <result name="fail">/error.jsp</result>
        </action>
        <action name="logoff" class="logoffAction" method="logoff">
            <result name="success" type="redirect">/login.jsp</result> 
        </action>
    </package>
</struts>

applicationContext-xxx.xml下面这四个其实可以放在一个配置档里,区分下来只是为了便于查找清晰点

applicationContext-action.xml
1 <bean id="loginAction" class="com.ivo.action.LoginAction" scope="prototype">
2         <property name="checkAuthorityService" ref="checkAuthorityService" />
3     </bean>

applicationContext-service.xml
1  <bean id="checkAuthorityService" class="com.ivo.service.impl.CheckAuthorityService">
2         <property name="uasUserDao" ref="uasUserDao" />
3     </bean>    

applicationContext-dao.xml
1 <bean id="uasUserDao" class="com.ivo.dao.impl.UasUserDao">
2         <property name="sqlMapClient" ref="sqlMapClient" />
3     </bean>

applicationCtontext-common.xml
 1 <bean id="mySqlDataSource"
 2         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 3         <property name="driverClassName">
 4             <value>com.mysql.jdbc.Driver</value>
 5         </property>
 6         <property name="url">
 7             <value>
 8                 jdbc:mysql://10.20.53.106:3306/qrademo
 9             </value>
10         </property>
11         <property name="username">
12             <value>root</value>
13         </property>
14         <property name="password">
15             <value>Ivo123</value>
16         </property>
17     </bean>
18 <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
19         <property name="configLocation">
20             <value>classpath:ibatis-config.xml</value>
21         </property>
22         <property name="dataSource">
23             <ref local="mySqlDataSource" />
24         </property>
25     </bean>

ibatis-config.xml
1 <sqlMapConfig>
2     <settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true"
3         maxRequests="300" maxSessions="200" maxTransactions="100" useStatementNamespaces="true" />
4
   <sqlMap resource="com/ivo/pojo/xml/UasUser.xml" />
5 </sqlMapConfig>

差不多建这些,当然还有许多jar包需要导



posted @ 2012-06-29 19:28 幻破 阅读(112) | 评论 (0)编辑 收藏
 
最近写了个功能jqgrid的某个栏位下载文件,本来直接用<a href=""/>直接链接的,但是出现了文件名含中文的就报错的现象,且若文件类型为.txt则直接打开。然后我纠结啊!
然后百度到网上有人使用struts2下载文件解决了这个问题,然后就借用了看看。
jquery代码
 1 function rangFun(value, options, rData){
 2           return '<a href=entCom/download.action?filename='+rData['range_id']+'>'+rData['range_id']+'</a>';
 3      }
 4  $("#externalListGrd").jqGrid({
 5          datatype    : "json",
 6          mtype       : "POST",
 7          width       : 720 ,
 8          height      : 420,
 9          rownumWidth : true,
10          shrinkToFit : false,
11          scroll      : true,
12         ondblClickRow:regectExtFun,
13          colNames    : ['允许范围'],
14          colModel:[
15             { name : 'range_id',   index : 'range_id',   width : 50, align: "left",sortable : "true",formatter:rangFun}]
16    })

struts.xml里的配置
1 <action name="download" class="com.ivo.action.DownloadAction">
2            <result name="success" type="stream">
3               <param name="contentType">text/html,application/vnd.ms-powerpoint,application/vnd.ms-word,application/vnd.ms-excel,text/plain</param> //文件打开方式
4               <param name="inputName">inputStream</param>
5               <param name="contentDisposition">attachment;filename="${filename}"</param>
6               <param name="bufferSize">2048</param>
7            </result>
8         </action>

后台代码
 1 package com.ivo.action;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.InputStream;
 5 import java.io.UnsupportedEncodingException;
 6 
 7 import com.opensymphony.xwork2.ActionSupport;
 8 
 9 public class DownloadAction extends ActionSupport {
10    private String filename;
11    public InputStream getInputStream() throws Exception{
12        String downloadFileName=filename;
13         try {
14             // downloadFileName = new String(downloadFileName.getBytes(),"UTF-8");这样带中文名的文件依旧会报错,想了半天找前辈帮忙看,后来改成下面的才OK。原因是从前台传过来的时候字符已经乱码,所以需要重新解析,再转换
15             downloadFileName = new String(downloadFileName.getBytes("ISO-8859-1"),"UTF-8");
16 
17         } catch(UnsupportedEncodingException e) {
18 
19             e.printStackTrace();
20 
21         }
22        String dir = "D:\\qra\\fileUpload\\"+downloadFileName;
23        return new FileInputStream(dir);
24    }
25 
26     public String getFilename() {
27         return filename;
28     }
29 
30     public void setFilename(String filename) {
31         this.filename = filename;
32     }
33    
34 }



posted @ 2012-06-29 19:08 幻破 阅读(177) | 评论 (0)编辑 收藏