This paper mainly explains how to use weinxinFundation for secondary development.

The steps are as follows:

Create a new Web project.

Create a new Dynamicly Web Project in Eclipse, such as weixinDemo in this article

2. The reference weinxinFundation

Get weinxinFundation methods: refer to my another article: www.cnblogs.com/vir56k/p/36…

There are two ways to refer to it: 1). If you have a JAR, refer to it in the normal way, as the programmer knows. 2). If you get the source code, please refer to my another article: http://www.cnblogs.com/vir56k/p/3778414.htmlCopy the code

3. Enter the background of wechat public platform, modify the token, and obtain appID and Secret.

3.1). Enter the background page of wechat public platform: mp.weixin.qq.com/

3.2). Find AppId and AppSecret under “Developer ID” and record them for later use.

3.3). In the “Server Configuration” section, set the URL and Token, where

Is behind you to publish servlet URL address, such as: XXX, XXX, XXX, XXX/weinxinDemo…

Note: here xxx.XXX.xxx. XXX is your IP or domain name, weinxinDemo is the name of your webSite, WeixinSvc is the name of sevlet.

Token is a Token used for authentication. You can set it anywhere, but note it down and use it later.

Note: In the demo, I use Helloweixin as my token

4. Create a new servlet (integrated with WeixinBaseServlet) to receive “Web calls from wechat services”.

Create a new servLet named WeixinSvc. Note that this has the same name as the servlet in 3.3 in the previous step. This servlet is integrated from: WeixinBaseServlet, with the following example code:

package weixinmobile.services; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import weixinFundation.core.common.WeixinBaseServlet; import weixinFundation.core.common.WeixinContext; @webservlet ("/ ** * ") public class extends WeixinBaseServlet {/** * @see HttpServlet#HttpServlet() */ public WeixinSvc() { super(); } @Override protected WeixinContext onInitWeixinContext() { return new WeixinContextImpl(); }}Copy the code

View Code

The above code writes a WeixinContextImpl that uses some “context configuration information,” which we implement.

package weixinmobile.services; import weixinFundation.core.common.LinkedMessageWeixinContext; import weixinmobile.services.handlers.EventMessageHandler; import weixinmobile.services.handlers.TextMessageHandler; public class WeixinContextImpl extends LinkedMessageWeixinContext{ public static final String Token = "helloweixin"; Public static final String appID = "your appID "; Public static final String appSecret = "your appsecret"; @Override public void onCreate() { addHander(new TextMessageHandler()); addHander(new EventMessageHandler()); } @Override public String getToken() { return Token; }}Copy the code

View Code

The onCreate of the above code loads two message handlers, as follows:

addHander(new TextMessageHandler()); AddHander (new EventMessageHandler()); // Message processing for handling events

Both TextMessageHandler and EventMessageHandler inherit from WeixinMessageHandler

Depending on your business needs, you can customize how these messages are handled. Your main business, code logic will be centralized in your custom Handler.

Your custom Handler handles messages from your phone or click events, user attention events, etc., where you can reply to them.

5. Configure and use the menu

According to the current rules, only the service number has the menu, which you need to open in the background when entering the wechat public platform.

Here’s how to create a menu according to my demo project weixinDemo,

Go to our project code and create a SRC /menu. TXT where the menu is described using JSON and you can modify it according to your needs.

You will see a MenuSvc servlet. In this servlet, I wrote a method to automatically obtain authentication, read the contents of the menu. TXT file and send it to the wechat background.

You need to execute a servlet in your browser in the following format:…. /MenuSvc? A =create is OK. This MenuSvc was written by me to facilitate the creation of menus. It is recommended to remove this MenuSvc when it is released to ensure security.

  

Release 6.

The compiled code is deployed in Tomcat to ensure the smooth flow of the external network. You can follow your own wechat public account.

 

 

 

My other articles:

Wechat public platform development – Basic chapter

Wechat public platform development – hands-on. Started the development of a wechat public platform with weinxinFundation