MVC(Android:intent-filter && Struts2)

来源:互联网 发布:java实现手机验证码 编辑:程序博客网 时间:2024/05/11 18:57

Android的Intent设计类似于Struts2框架中逻辑视图的设计,Intent通过指定Action属性把Intnet与具体的Activity分离,从而进行解耦,

接下来通过代码来进行比较:

Android的简单实现:

package com.anthony.intent;import com.anthony.intent.R;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class AndroidMVCActivity extends Activity {    /** Called when the activity is first created. */final static String ANTHONY_ACTION = "com.anthony.intent.action.ANTHONY_ACTION";final static String ANTHONY_CATEGORY = "com.anthony.intent.category.ANTHONY_CATEGORY";@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);Button bn = (Button)findViewById(R.id.bn);bn.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0){Intent intent = new Intent();//设置Action属性intent.setAction(AndroidMVCActivity.ANTHONY_ACTION);//添加Category属性intent.addCategory(AndroidMVCActivity.ANTHONY_CATEGORY);startActivity(intent);}});}}


 

package com.anthony.intent;import java.util.Set;import com.anthony.intent.R;import android.app.Activity;import android.os.Bundle;import android.widget.EditText;/** *  * @author Anthony_YWJ * */public class SecondActivity extends Activity{@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.second);EditText show = (EditText)findViewById(R.id.show);String action = getIntent().getAction();show.setText("Action为:" + action);EditText cate = (EditText)findViewById(R.id.cate);Set<String> cates = getIntent().getCategories();cate.setText("Category属性为:" + cates);}}


 

Android的配置:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.anthony.intent"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="8" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:name="com.anthony.intent.AndroidMVCActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity android:name=".SecondActivity"android:label="@string/app_name"><intent-filter><!-- 指定该Activity能响应action为指定字符串的Intent --><action android:name="com.anthony.intent.action.ANTHONY_ACTION" /><!-- 指定该Activity能响应category为指定字符串的Intent --><category android:name="com.anthony.intent.category.ANTHONY_CATEGORY" /><!-- 指定该Activity能响应category为android.intent.category.DEFAULT的Intent --><category android:name="android.intent.category.DEFAULT" />   </intent-filter></activity>    </application></manifest>


 Struts2的简单实现:

1.简单的表单:

<%@ page contentType="text/html; charset=gbk"%><%@taglib prefix="s" uri="/struts-tags"%><html>  <head>    <title><s:text name="title.text"/></title>  </head>    <body>    <s:form action="sayhello">    <!-- 通过struts.xml配置文件去找到对应的类来处理提交的信息 -->    <s:text name="label.name"/>    <s:textfield name="userName" size="22"/>    <s:submit key="label.submit"/>     <s:reset key="label.reset"/>    </s:form><s:if test="hasFieldErrors()"><s:fielderror/></s:if>   </body></html>

struts.xml配置
<?xml version="1.0" encoding="gbk"?><!DOCTYPE struts PUBLIC        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"        "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><constant name="struts.i18n.encoding" value="gbk"/><constant name="struts.locale" value="zh_CN" /><constant name="struts.custom.i18n.resources" value="messageResource"/><constant name="struts.devMode" value="true" /><constant name="struts.ui.theme" value="simple" />    <package name="hello" extends="struts-default"><!-- 配置业务控制器HelloAction映射 --><action name="sayhello" class="com.anthony.action.HelloAction"><result>/welcome.jsp</result><result name="input">/index.jsp</result></action>    </package></struts>


 

package com.anthony.action;public class HelloAction extends ActionSupport{private String userName;//保存请求参数用户姓名private String reslutStr;//存放处理结果/** 重载ActionSupport类的execute方法 */public String execute(){reslutStr="这是业务控制器HelloAction处理的结果内容!";return SUCCESS;}/** 手动进行表单验证 */public void validate(){//用户姓名不能为空if(userName==null||userName.trim().length()<1){addFieldError("userName",getText("username.error"));}}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getReslutStr() {return reslutStr;}public void setReslutStr(String reslutStr) {this.reslutStr = reslutStr;}}