Struts2+Android 使用struts2制作做WebService


去青软那边,认识到自己的不足,只做Android是不行的.前几天公司也让做服务器.于是今天开始拿起javaEE 以后还是好好做JavaEE+Android吧

看了一下黎老师的WebService,还是很典型的应用(黎老师的课程确实很棒啊!受益一生),可惜的是他用的struts做的 也是今天中午移植到struts2 也算是练手+重温了.

进正题>

做Struts2 首先是配置工程 这个很烦人,和Android比差的很远.

首先是

web.xml没什么好说的其实就是配置struts2

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.   <display-name></display-name>   
  8.   <welcome-file-list>  
  9.     <welcome-file>index.jsp</welcome-file>  
  10.   </welcome-file-list>  
  11.     
  12.   <filter>  
  13.     <!-- 定义核心Filter的名称 -->  
  14.     <filter-name>struts2</filter-name>  
  15.     <!--定义核心Filter的实现类 -->  
  16.     <filter-class>  
  17.         org.apache.struts2.dispatcher.FilterDispatcher  
  18.     </filter-class>  
  19. </filter>  
  20.   
  21. <filter-mapping>  
  22.     <!--核心Filter的名称 -->  
  23.     <filter-name>struts2</filter-name>  
  24.     <!--使用该核心Filter来接受所有的Web请求 -->  
  25.     <url-pattern>/*</url-pattern>  
  26. </filter-mapping>  
  27. </web-app>  

struts.xml相当于Android中的AndroidManifest.xm

l 就一个action,返回两个结果,json和xml 貌似Android中现在很流行json的WebService

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2.   
  3. <!DOCTYPE struts PUBLIC  
  4.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  5.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  6.   
  7. <struts>  
  8.     <package name="struts2" extends="struts-default">  
  9.         <action name="List" class="com.su.action.VideoListAction">  
  10.             <result name="xml">/videos.jsp</result>  
  11.             <result name="json">/jsonvideos.jsp</result>  
  12.         </action>  
  13.     </package>  
  14. </struts>  

然后就是结果返回页面,先看xml的:跳转到videos.jsp 注意这里有一个struts的迭代器 可以把获取的videos处理后输出

  1. <%@ page language="java" contentType="text/xml; charset=UTF-8" pageEncoding="UTF-8"%><%@taglib prefix="s" uri="/struts-tags"%><?xml version="1.0" encoding="UTF-8"?>  
  2. <videos>   
  3. <s:iterator value="#request.videos" id="video">  
  4.     <video id="<s:property value="#video.id"/>">  
  5.     <title><s:property value="#video.title"/></title>  
  6.     <timelength><s:property value="#video.time"/></timelength>  
  7.     </video>  
  8.     </s:iterator>   
  9. </videos>  

如果返回的是json那么是jsonviedos.jsp

  1. <%@ page language="java" contentType="text/plain; charset=UTF-8" pageEncoding="UTF-8"%>${videos}  

需要注意!xml文件中 

<%@ page language="java" contentType="text/xml; charset=UTF-8" pageEncoding="UTF-8"%><%@taglib prefix="s" uri="/struts-tags"%><?xml version="1.0" encoding="UTF-8"?>

这里尖括号直接不要有空格不然在chrome里不能识别为xml文件 我想在解析的时候会报错(什么没有文件头什么的吧)

然后是java代码部分了

首先是VideoListAction.java也就是主action 相当于activity了

  1. package com.su.action;  
  2.   
  3. import java.io.OutputStream;  
  4. import java.io.PrintWriter;  
  5. import java.util.List;  
  6.   
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9.   
  10. import org.apache.struts2.ServletActionContext;  
  11.   
  12. import com.opensymphony.xwork2.ActionContext;  
  13. import com.opensymphony.xwork2.ActionSupport;  
  14. import com.su.domain.Video;  
  15. import com.su.service.VideoService;  
  16. import com.su.service.impl.VideoServiceBean;  
  17.   
  18. public class VideoListAction extends ActionSupport {  
  19.     private VideoService service = new VideoServiceBean();  
  20.     private String format;  
  21.     public String getFormat() {  
  22.         return format;  
  23.     }  
  24.     public void setFormat(String format) {  
  25.         this.format = format;  
  26.     }  
  27.       
  28.     @Override  
  29.     public String execute() throws Exception {  
  30.         List<Video> videos = service.getLastVideos();  
  31.         if (format.equals("json")) {  
  32.             StringBuilder json = new StringBuilder();  
  33.             json.append('[');  
  34.             for(Video video : videos){ // {id:76,title:"xxxx",timelength:80}   
  35.                 json.append('{');  
  36.                 json.append("id:").append(video.getId()).append(',');  
  37.                 json.append("title:\"").append(video.getTitle()).append("\",");  
  38.                 json.append("timelength:").append(video.getTime());  
  39.                 json.append('}').append(',');  
  40.             }  
  41.             json.deleteCharAt(json.length()-1);  
  42.             json.append(']');  
  43.             ServletActionContext.getRequest().setAttribute("videos", json);  
  44.             System.out.println("1111111111111111111111111111");  
  45.             return "json";  
  46.         }  
  47.         else {  
  48.         ServletActionContext.getRequest().setAttribute("videos", videos);  
  49.         return "xml";  
  50.         }  
  51.           
  52.     }  
  53. }  

很简单json就是从videos里拼接把String放到servleactioncontext  ; xml就更简单了,直接返回的list

  • 1
  • 2
  • 3
  • 下一页

相关内容