play框架资料
http://www.021128.net/2011/05/29/playframeworkintro/
http://www.360doc.com/content/12/0117/12/834950_179878119.shtml
pubsub资料
http://www.igniterealtime.org/builds/smack/docs/latest/documentation/extensions/index.html
又接触到play框架,加深了一下了解,总的来说还是非常方便好用的,可以使用eclipse调试,直接把调试信息输出在eclipse控制台,修改文件之后不用手动重启项目。
配置好环境变量后,可以用这句来添加eclipse所需要的配置文件,然后再eclipse中import该项目就可以。
play eclipsify your_app_name如果从别处下载来的项目可以用,这句把一些依赖项加上。
play dependencies your_app_name
打开help这样
play help
get方法 获取参数只要方法参数名和这个HTTP的参数名一样就可以了
比如http请求
/articles/archive?date=08/01/08&page=2
public static void archive(Date date, Integer page) { List articles = Articles.fromArchive(date, page); render(articles); }
<form action="/Directory/add" method="POST"> Name: <input name="p.name" type="text" /> Age: <input name="p.age" type="text" /> </form>
public class Person { public String name; public Integer age; } public static void add(Person p) { p.save(); }
GET / application.index这一行说明直接访问 locahost:9000 的时候服务器会指向 Application 这个 controller 的index方法
一般这个index方法是这样,render 渲染器 负责显示页面,会自动会寻找index.html,MVC模型下外界操作不允许直接访问view模型
public static void index() { render(); }如果想静态访问页面必须这样写
GET /home staticFile:/public/html/index.html或者,此目录下的内容都可以访问
GET /public/ staticDir:public
# Catch all * /{controller}/{action} {controller}.{action}当然也可以填别的
比如这个方法
public static void run(long num){ render(num); }
<a href="@{Application.run(1)}">run</a>routes里面写
GET /index/{num} Application
这样就可以使用这种模式的URL http://localhost:9000/index/1
在页面的时候可以用 ${n} 来访问数据。文件上传也很简单
#{form @uploadPhoto(), enctype:'multipart/form-data'} <input type="text" name="title" /> <input type="file" id="photo" name="photo" /> <input type="submit" value="Send it..." /> #{/}
public static void uploadPhoto(String title, File photo) { ... }这里要注意 enctype:'multipart/form-data'这一句要加上,不然post包中只会包含文件名不会包含内容'
具体看这里
http://www.w3school.com.cn/tags/att_form_enctype.asp
js 等脚本不具有操作本地文件的功能,必须依靠浏览器提供的activeOBject,但是每个浏览器提供的object一般来说是不同的,
一般上传文件只能用input标签的file
最后说一下,xmpp发送自定义包,直接上代码了
import org.jivesoftware.smack.ConnectionConfiguration; import org.jivesoftware.smack.PacketListener; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.filter.PacketTypeFilter; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ.Type; import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.provider.IQProvider; import org.jivesoftware.smack.provider.ProviderManager; import org.xmlpull.v1.XmlPullParser; public class smackDemo { private static XMPPConnection con; public static void main(String[] args) throws Exception{ LogIn(); sendData(); while(true){ } } public static void LogIn() throws Exception{ XMPPConnection.DEBUG_ENABLED=true; ProviderManager.getInstance().addIQProvider(MyIQ.ELEMENT,MyIQ.NAMESPACE,new MyIQProvider()); con = new XMPPConnection(new ConnectionConfiguration("127.0.0.1",5222)); con.connect(); con.login("1", "1"); con.addPacketListener(new MyPacketListener(),new PacketTypeFilter(MyIQ.class)); } public static class MyPacketListener implements PacketListener{ @Override public void processPacket(Packet p){ //System.out.println(p.toXML()); MyIQ iq = (MyIQ)p; System.out.println(iq.getData()); try{ Base64Parser.writeRawData(iq.getData()); }catch(Exception ex){ } System.out.println("write finish"); } } public static void sendData()throws Exception{ MyIQ myIQ = new MyIQ(); myIQ.setData(Base64Parser.getBase64()); myIQ.setTo(con.getUser()); myIQ.setFrom(con.getUser()); myIQ.setType(Type.SET); con.sendPacket(myIQ); Thread.sleep(50000); con.disconnect(); } public static class MyIQ extends IQ{ public static final String ELEMENT = "MyIQ"; public static final String NAMESPACE = "MyIQ.com"; public static final String TAGNAME = "DATA"; private String data=""; public String getData(){ return this.data; } public void setData(String data){ this.data=data; } @Override public String getChildElementXML(){ StringBuffer sb = new StringBuffer(); sb.append("<").append(ELEMENT).append(" xmlns=\"").append(NAMESPACE).append("\">"); sb.append("<DATA>").append(data).append("</DATA>"); sb.append("</").append(ELEMENT).append(">"); return sb.toString(); } } public static class MyIQProvider implements IQProvider { @Override public IQ parseIQ(XmlPullParser xp) throws Exception { MyIQ result = new MyIQ(); int n ; do { n = xp.next(); if (n == XmlPullParser.START_TAG) { if ("DATA".equals(xp.getName())) result.setData(xp.nextText()); } }while (n != XmlPullParser.END_TAG || !"MyIQ".equals(xp.getName()) ); return result; } } }
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStream; import org.jivesoftware.smack.util.Base64; public class Base64Parser { public static String getBase64() throws Exception{ File file = new File("1.jpg"); FileInputStream fis = new FileInputStream(file); byte[] b = new byte[fis.available()]; fis.read(b); fis.close(); return Base64.encodeBytes(b); } public static void writeRawData(String base64Code) throws Exception{ File file = new File("2.jpg"); FileOutputStream fos = new FileOutputStream(file); fos.write(Base64.decode(base64Code)); fos.close(); return; } }