我是靠谱客的博主 勤奋皮皮虾,这篇文章主要介绍一、搭建移动应用服务器,现在分享给大家,希望可以做个参考。

搭建一个简单的安卓应用的服务端。采用Struts2+hibernate的架构。IDE使用eclipse,数据库使用MySQL。搭建java开发环境就不说了,百度一大把。

一、项目结构

本人主要是android开发,了解一点java服务器开发,借用这个机会熟悉一下服务端开发。个人感觉使用Struts就是配置麻烦,先把整个项目的结构图PO在下面,配置随后附上。

项目结构如下图所示:



二、项目介绍

1、数据库hibernate配置:hibernate.cfg.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.bytecode.use_reflection_optimizer">false</property> <property name="hibernate.c3p0.acquire_increment">2</property> <property name="hibernate.c3p0.idle_test_period">120</property> <property name="hibernate.c3p0.max_size">50</property> <property name="hibernate.c3p0.max_statements">0</property> <property name="hibernate.c3p0.min_size">2</property> <property name="hibernate.c3p0.timeout">120</property> <property name="hibernate.c3p0.validate">true</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/keng</property> <property name="hibernate.connection.username">admin</property> <property name="hibernate.connection.password">admin</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.search.autoregister_listeners">false</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.validator.apply_to_ddl">false</property> <mapping class="com.hy.keng.data.User" /> </session-factory> </hibernate-configuration>

2、Struts配置:struts.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="true" /> <constant name="struts.action.extension" value="ss" /> <constant name="struts.i18n.encoding" value="UTF-8" /> <constant name="struts.allowed.action.names" value="[a-zA-Z0-9_]{3,50}" /> <constant name="struts.multipart.saveDir" value="/tmp"></constant> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.multipart.maxSize" value="100000000" /> <constant name="struts.ognl.allowStaticMethodAccess" value="true" /> <package name="v1_user_json" extends="json-default" namespace="/client"> <action name="*_*_j" class="com.hy.keng.action.{1}" method="{2}"> <result name="json" type="json"> <param name="ignoreHierarchy">false</param> <param name="excludeProperties">texts,actionErrors,actionMessages,errorMessages,errors,fieldErrors,locale</param> <param name="noCache">true</param> <param name="excludeNullProperties">true</param> <param name="contentType">text/plain</param> </result> <!-- 返回JSONP格式,解决跨域问题 --> <result name="error">errorjson.jsp</result> </action> <action name="*_j" class="com.hy.keng.action.{1}"> <result name="json" type="json"> <param name="ignoreHierarchy">false</param> <param name="excludeProperties">texts,actionErrors,actionMessages,errorMessages,errors,fieldErrors,locale</param> <param name="noCache">true</param> <param name="excludeNullProperties">true</param> <param name="contentType">text/plain</param> </result> <result name="error">errorjson.jsp</result> </action> </package> </struts>

3、web配置web.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" metadata-complete="true" version="3.0"> <display-name>keng</display-name> <filter> <filter-name>ExpiresFilter</filter-name> <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class> <init-param> <param-name>ExpiresByType text/html</param-name> <param-value>access plus 0 seconds</param-value> </init-param> </filter> <filter-mapping> <filter-name>ExpiresFilter</filter-name> <url-pattern>*.ss</url-pattern> </filter-mapping> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.ss</url-pattern> </filter-mapping> </web-app>

三、数据库介绍

数据库使用的是MySQL,数据库管理工具用的是heidisql。

mysql的安装和配置过程我就不说了,打开heidisql,输入本机ip和账号密码


进入后新建数据库keng,然后再新建一张数据表user,建表语句如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
CREATE TABLE `user` ( `id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'ID', `username` VARCHAR(50) NOT NULL COMMENT '用户名' COLLATE 'utf8mb4_unicode_ci', `nickname` VARCHAR(50) NULL DEFAULT NULL COMMENT '昵称' COLLATE 'utf8mb4_unicode_ci', PRIMARY KEY (`id`), INDEX `username` (`username`) ) COMMENT='用户表' COLLATE='utf8mb4_unicode_ci' ENGINE=InnoDB AUTO_INCREMENT=2;

然后添加两条数据进去就可以测试了。



四、测试

打开浏览器,输入地址,正确结果如下:


用户不存在结果如下:


demo下载地址附上:http://download.csdn.net/detail/yu75567218/9586917


最后

以上就是勤奋皮皮虾最近收集整理的关于一、搭建移动应用服务器的全部内容,更多相关一、搭建移动应用服务器内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(88)

评论列表共有 0 条评论

立即
投稿
返回
顶部