在applicationContext.xml里寫:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

">
<!-- 使用注解支持事務 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 添加jdbc的任務管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///mydb"/>
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">root</prop>
</props>
</property>
</bean>
<bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="dataSource"></constructor-arg>
</bean>
<!-- 在使用事務時不能配置jdbc模板,只能配置數據流 -->
<bean id="userdao" class="com.yjw.dao.UserDao">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userService" class="com.yjw.service.UserService">
<!-- name的值只和set方法后面的有關,要一樣 -->
<property name="dao" ref="userdao"></property>
</bean>
</beans>在service里加:
//在類的頭上加上這個注解,代表這個類的所有方法都加上了事務,
//作用是某個方法里的代碼要么都執行,要么都不執行
//默認是在發生RunTimeException是才回滾,發生Exception不回滾
//加上(rollbackFor=Exception.class)表示所有的異常都回滾
@Transactional(rollbackFor=Exception.class)

public class UserService
{

private UserDao userdao;


public void setDao(UserDao userdao)
{
this.userdao = userdao;
}

public void save(User user)
{
userdao.save(user);
}
//只讀,會使性能提高,推薦使用
@Transactional(readOnly=true)

public User findById(int id)
{
return userdao.findById(id);
}
}

摘要: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->在applicationContext.xml文件里寫:<?xml version="1.0" encoding="UTF-8"?><beans xmlns...
閱讀全文
1,添加jar包struts2-json-plugin-2.3.1.2.jar
2.在json-struts.xml里配置
<package name="myjson" extends="json-default">
<action name="myjson" class="com.yjw.web.MyjsonAction">
<result type="json">
<!--找到根節點-->
<param name="root">user</param>
<!--瀏覽器不要緩存-->
<param name="noCache">true</param>
<!--GZIP網頁壓縮協議,可以讓傳送更快,省流量-->
<param name="enableGZIP">true</param>
<!--排除action里是null的屬性-->
<param name="excludeNullProperties">true</param>
</result>
</action>
</package>
在MyjsonAction里寫:
package com.yjw.web;

import com.opensymphony.xwork2.Action;


public class MyjsonAction implements Action
{
private User user;
private String x;


public String execute() throws Exception
{
user = new User();
user.setId(1);
user.setMoney(22);
user.setName("tom");
return "success";
}


public User getUser()
{
return user;
}


public void setUser(User user)
{
this.user = user;
}


public String getX()
{
return x;
}


public void setX(String x)
{
this.x = x;
}

}

在struts2.Xml中寫:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="myxml" extends="struts-default">
<!-- 輸出xml的方法不寫result -->
<action name="xml" class="com.yjw.web.MyxmlAction"></action>
</package>
</struts>
在action中寫:
package com.yjw.web;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;


public class MyxmlAction implements Action
{


public String execute() throws Exception
{
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/xml;charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.print("<root>");

for(int i=0;i<5;i++)
{
out.print("<person>");
out.print("<name>person"+i+"</name>");
out.print("</person>");
}
out.print("</root>");
//一定要返回null
return null;
}

}
在jsp里寫:
<body>
<input type="button" value="xml" id="btnxml"/>
<div id="mydiv"></div>
<script type="text/javascript" src=js/jquery-1.5.1.min.js></script>
<script type="text/javascript">

$(document).ready(function()
{

$("#btnxml").click(function()
{

$.get("xml.action",function(xml)
{

$(xml).find("person").each(function()
{
var name = $(this).find("name").text();
$("#mydiv").html($("#mydiv").html()+name+'<br/>');
});
});
});
});
</script>
</body>

在jsp里寫:
<a href="download.action">點此下載文件</a>

在struts.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="mydown" extends="struts-default">
<action name="download" class="com.yjw.web.DownAction">
<param name="id">1</param>
<result type="stream">
<!-- 下載文件的mime類型 -->

<param name="contentType">$
{fileType}</param>
<!-- 下載文件的描述 -->

<param name="contentDisposition">attachment;filename=$
{fileName}</param>
<!-- 設置緩沖區大小 -->
<param name="bufferSize">1024</param>
<!-- 獲得文件名的getxxx方法的名字 ,不包含get-->
<param name="inputName">inputStream</param>
</result>
</action>
</package>
</struts>
在action中的程序:
package com.yjw.web;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;

public class DownAction implements Action
{
private String fileName;
private String id;
private String fileType;

public String execute() throws Exception
{
return "success";
}

public InputStream getInputStream() throws IOException
{
String path = ServletActionContext.getServletContext().getRealPath("/");

if(id.equals("1"))
{
path = path + "download/less.pdf";
fileName = "css.pdf";
fileType = "application/pdf";

} else
{
path = path + "download/data.xlsx";
fileName = "data.xlsx";
fileType = "application/vnd.ms-excel";
}
FileInputStream stream = new FileInputStream(new File(path));
return stream;
}

public String getFileName()
{
return fileName;
}

public void setFileName(String fileName)
{
this.fileName = fileName;
}

public String getId()
{
return id;
}

public void setId(String id)
{
this.id = id;
}

public String getFileType()
{
return fileType;
}

public void setFileType(String fileType)
{
this.fileType = fileType;
}
}

HSSFWorkbook wb = new HSSFWorkbook();
FileOutputStream fileout = new FileOutputStream("d:/b.xls");
wb.write(fileout);
fileout.close();

驗證碼:
1, 導入jar包jcaptcha驗證碼
2.在web.xml里配置<servlet>
<servlet-name>j</servlet-name>
<servlet-class>com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>j</servlet-name>
<url-pattern>/jcaptcha.jpg</url-pattern>
</servlet-mapping>
3.在頁面上寫
<form action="vali.action" method="post">
<input type="text" name="name"/>
<a href="javascript:void(0)"
id="mya"><img src="jcaptcha.jpg" id="myimg"/></a>
<input type="submit" value="save"/>
</form>
<script type="text/javascript" src=js/jquery-1.5.1.min.js></script>
<script type="text/javascript">
$(document).ready(function(){
$("#mya").click(function(){
$("#myimg").attr("src","jcaptcha.jpg?xxx=" + Math.random());
});
4.在struts-vali.xml里寫
<?xml version="1.0"
encoding="UTF-8"?>
<!DOCTYPE struts
PUBLIC
"-//Apache Software Foundation//DTD Struts
Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="myvali"
extends="struts-default">
<action name="vali" class="com.yjw.web.ValiAction">
<result>WEB-INF/views/list.jsp</result>
<result name="error">WEB-INF/views/main.jsp?id=1</result>
</action>
</package>
</struts>
5.在ValiAction里寫:
package com.yjw.web;
import org.apache.struts2.ServletActionContext;
import
com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet;
import com.opensymphony.xwork2.Action;
public class ValiAction implements Action{
private String name;
public String execute() throws Exception {
boolean result = SimpleImageCaptchaServlet.validateResponse(ServletActionContext.getRequest(),
name);
System.out.println(result);
if(result){
return "success";
}else {
return "error";
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在javaIO中,文件的查詢和刪除,文件的復制程序如下:
普通的復制是:
public class Acopy {
public void copy(String oldpath,String newpath ) throws IOException {
File of = new File(oldpath);
File nf = new File(newpath);
if(!nf.exists()){
nf.createNewFile();
}
FileInputStream i = new FileInputStream(of);
FileOutputStream o = new FileOutputStream(nf);
int b= 0;
byte[] buffer = new byte[100];
while((b=i.read(buffer))!=-1){
o.write(buffer, 0, b-1);
}
i.close();
o.flush();
o.close();
}
}
加強的復制是:
public class Bcopy {
public void copy(String opath,String npath) throws IOException{
File of = new File(opath);
File nf = new File(npath);
if(!nf.exists()){
nf.createNewFile();
}
FileInputStream i = new FileInputStream(of);
BufferedInputStream bi = new BufferedInputStream(i);
FileOutputStream o = new FileOutputStream(nf);
BufferedOutputStream bo = new BufferedOutputStream(o);
int b = 0;
byte[] buffer = new byte[100];
while((b=bi.read(buffer))!=-1){
bo.write(buffer, 0, b-1);
}
bi.close();
bo.flush();
bo.close();
}
}
文件的查詢是:
public void show(String path){
File f = new File(path);
if(f.isFile()){
System.out.println(f.getPath());
}else if(f.isDirectory()){
File[] files = f.listFiles();
if(files!=null){
for(File file : files){
if(file.isFile()){
System.out.println(file.getPath());
}else if(file.isDirectory()){
System.out.println("["+file.getPath()+"]");
show(file.getPath());
}
}
}
}
}
文件的刪除是:
public void del(String path){
File f = new File(path);
if(f.isFile()){
f.delete();
}else if(f.isDirectory()){
File[] files = f.listFiles();
if(files.length==0){
f.delete();
}else if(files!=null){
for(File file : files){
if(file.isFile()){
file.delete();
}else if(file.isDirectory()){
del(file.getPath());
}
}
}
}
f.delete();
}
在建立線程時,有兩種方式,1是繼承java.lang.Thread類,重寫run方法,此方法用于你想讓線程干的事,在調用此方法時,不能直接調用run方法,要調用start方法,如果想訪問此線程,使用Thread.currentThread()方法。2,是實現Runnable接口,在調用時,因為接口沒有start方法,所以要先創建出實現類的的對象, 再創建出Thread的對象,把實現類的對象傳入Thread的對象的構造方法中,再用Thread的對象調用start()方法。
線程有生命周期的,新建 ,就緒,運行,死亡。新建以后只有調用start方法才處于就緒隊列,處于就緒隊列的線程在獲得處理機資源后處于運行,在運行過程中,如果調用sleep方法,或IO阻塞例如Scanner輸入,或等待同步鎖,或等待通知時就處于阻塞,這是釋放處理機資源,在sleep時間到,或IO方法返回,或獲得同步鎖,或受到通知后,處于就緒隊列,線程在run執行完成,或Error,或exeception時,就死亡。可以調用stop方法讓線程死亡,但是禁止使用,可以使用isAlive方法判斷線程是否死亡,該方法在線程處于就緒,運行,阻塞時返回true,處于新建和死亡時返回false,線程一旦死亡,不能再次調用start方法讓線程重新執行。
當一個程序在執行中被另外一個線程插隊,并且后來的線程需要先執行完后原來的線程才能執行,就要讓插隊線程先調用start方法,再調用join方法,進行插隊。
守護線程就是在主線程死亡后,守護線程也隨之死亡,在使用時讓守護線程的對象先調用setDaemon(true)方法,再調用start方法。
當多個線程同時對同一個對象的實例屬性進行操作時,會引起線程的同步問題,為了消除線程的同步,可用synchronized來修飾有可能引起線程同步的方法,也可用用synchronized(this)
{方法代碼} ,設置同步代碼塊,在JDK1.5以上的版本可使用同步鎖來操作,具體做法是:
Private final ReentrantLock lock = new ReentrantLock();
在方法內部定義lock.lock();在程序執行完畢后,一定要解鎖,lock.unlock();
一,復制文件
File oldFile = new File("F:/FavoriteVideo/0CAMGLN0K.jpg");
File newFile = new File("F:/FavoriteVideo/yang.jpg");
if(!oldFile.exists()){
newFile.createNewFile();
}
FileInputStream input = new FileInputStream(oldFile);
/*如果你想讓文件的復制加快BufferedInputStream bufferedInput = new BufferedInputStream(input);*/
FileOutputStream output = new FileOutputStream(newFile );
/*BufferedOutputStream bufferedOut = new BufferedOutputStream(output);*/
byte[] buffer = new byte[512];
int b = 0;
long startTime = System.currentTimeMillis();
while(b!=-1){
b=input.read(buffer);
output.write(buffer, 0, buffer.length);
}
long endTime = System.currentTimeMillis();
System.out.println(endTime-startTime);
input.close();
output.flush();
output.close();
二文件的查詢方法
public class ShowFilePath {
public void show(String path){
File f = new File(path);
if(f.isFile()){
System.out.println(f.getPath());
}else if(f.isDirectory()){
File[] files = f.listFiles();
if(files!=null){
for(File file:files ){
if(file.isFile()){
System.out.println(file.getPath());
}else {
System.out.println("["+file.getPath()+"]");
show(file.getPath());
}
}
}
}
}