歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Struts2+Android 使用struts2制作做WebService

Struts2+Android 使用struts2制作做WebService

日期:2017/3/1 10:22:21   编辑:Linux編程

去青軟那邊,認識到自己的不足,只做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. <filter>
  12. <!-- 定義核心Filter的名稱 -->
  13. <filter-name>struts2</filter-name>
  14. <!--定義核心Filter的實現類 -->
  15. <filter-class>
  16. org.apache.struts2.dispatcher.FilterDispatcher
  17. </filter-class>
  18. </filter>
  19. <filter-mapping>
  20. <!--核心Filter的名稱 -->
  21. <filter-name>struts2</filter-name>
  22. <!--使用該核心Filter來接受所有的Web請求 -->
  23. <url-pattern>/*</url-pattern>
  24. </filter-mapping>
  25. </web-app>

struts.xml相當於Android中的AndroidManifest.xm

l 就一個action,返回兩個結果,json和xml 貌似Android中現在很流行json的WebService

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

很簡單json就是從videos裡拼接把String放到servleactioncontext ; xml就更簡單了,直接返回的list

Copyright © Linux教程網 All Rights Reserved