一.创建WEB工程,应用STRUTS(本例只作简单测试未与数据库连接)
处理类1:
package action;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import bussness.UserInfoBo;
import entity.UserInfoForm;
public final class LogonAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
UserInfoForm userInfoForm = (UserInfoForm) form;
//从web层获得用户名和口令
String username = userInfoForm.getUsername().trim();
String password = userInfoForm.getPassword1().trim();
//声明错误集对象
ActionErrors errors = new ActionErrors();
//校验输入
if(username.equals("")){
ActionError error=new ActionError("error.missing.username");
errors.add(ActionErrors.GLOBAL_ERROR,error);
}
if(password.equals("")){
ActionError error=new ActionError("error.missing.password");
errors.add(ActionErrors.GLOBAL_ERROR,error);
}
PrintWriter out = response.getWriter();
//调用业务逻辑
if(errors.size()==0){
String validated = "";
try{
UserInfoBo userInfoBo=new UserInfoBo();
validated =userInfoBo.validatePwd(username,password);
if(validated.equals("match")){
//一切正常就保存用户信息并转向成功的页面
HttpSession session = request.getSession();
session.setAttribute("userInfoForm", form);
out.print("aaaaa");
// return mapping.findForward("success");
return mapping.findForward("success2");
}
}
catch(Throwable e){
//处理可能出现的错误
e.printStackTrace();
ActionError error=new ActionError(e.getMessage());
errors.add(ActionErrors.GLOBAL_ERROR,error);
}
}
//如出错就转向输入页面,并显示相应的错误信息
saveErrors(request, errors);
out.print("bbbbb");
// return new ActionForward(mapping.getInput());
return mapping.findForward("success2");
}
}
处理类2:
package bussness;
import entity.UserInfoForm;
public class UserInfoBo {
public UserInfoBo(){
}
public String validatePwd(String username,String password){
String validateResult="";
if(username.equals("test")&&password.equals("test")){
validateResult="match";
System.out.println("登录成功: "+username);
}
else{
System.out.println("登录失败: "+username);
throw new RuntimeException("error.noMatch");
}
return validateResult;
}
}
处理类3:
# Resources for parameter 'com.yourcompany.struts.ApplicationResources'
# Project testFlex
#Application Resource for the logon.jsp
logon.jsp.title=The logon page
logon.jsp.page.heading=Welcome World!
logon.jsp.prompt.username=Username:
logon.jsp.prompt.password=Password:
logon.jsp.prompt.submit=Submit
logon.jsp.prompt.reset=Reset
#Application Resource for the main.jsp
main.jsp.title=The main page
main.jsp.welcome=Welcome:
#Application Resource for the LogonAction.java
error.missing.username=<li><font color="red">missing username</font></li>
error.missing.password=<li><font color="red">missing password</font></li>
#Application Resource for the UserInfoBo.java
error.noMatch=<li><font color="red">no matched user</font></li>
处理类4:
package entity;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
public class UserInfoForm extends ActionForm{
private String username;
private String password1;
public String getUsername() {
return (this.username);
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword1() {
return (this.password1);
}
public void setPassword1(String password1) {
this.password1 = password1;
}
}
配置STRUTS.XML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans>
<form-bean name="userInfoForm" type="entity.UserInfoForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action input="/logon.jsp" name="userInfoForm" path="/logonAction" scope="session"
type="action.LogonAction" validate="false">
<forward name="success" path="/main.jsp" />
<forward name="success2" path="/MyJsp.jsp"/>
</action>
</action-mappings>
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>
配置WEB.XML:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
配置JSP:(MyJsp.jsp)
<%@page import="java.util.List"%>
<%
response.setContentType("text/xml");
out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
out.println("<userinfo>");
out.println("<user userId=\""+3123+"\">");
out.println("<username>"+42314+"</username>");
out.println("<userpwd>"+3123+"</userpwd>");
out.println("<useremail>"+4231+"</useremail>");
out.println("<userage>"+2+"</userage>");
out.println("<regdate>"+4231+"</regdate>");
out.println("</user>");
out.println("</userinfo>");
%>
二.FLEX端处理
FLEX通过HTTPSERVICE与WEB项目通迅代码如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
backgroundColor="#FFFFFF"
backgroundAlpha="0" horizontalCenter="true" >
<mx:HTTPService id="info" url="http://127.0.0.1:8080/testFlex/logonAction.do">
<mx:request>
<username>
{username.text}
</username>
<password1>
{password.text}
</password1>
</mx:request>
</mx:HTTPService>
<mx:TitleWindow id ="loginWindow" xmlns:mx="http://www.adobe.com/2006/mxml" title="Logon" x="350" y="80">
<mx:Form>
<mx:FormItem label="Lusername" required="true">
<mx:TextInput id="username" width="150">
</mx:TextInput>
</mx:FormItem>
<mx:FormItem label="LPassword" required="true">
<mx:TextInput id="password" width="150" displayAsPassword="true"/>
</mx:FormItem>
<mx:FormItem>
<mx:HBox horizontalGap="30">
<mx:Button label="Logon" click="info.send();" />
<mx:Button label="Cancel" click="info.send();"/>
</mx:HBox>
</mx:FormItem>
</mx:Form>
</mx:TitleWindow>
<mx:DataGrid dataProvider="{info.lastResult.userinfo.user}" width="60%" x="160" y="300" >
<mx:columns>
<mx:Array>
<mx:DataGridColumn headerText="姓名" dataField="username"/>
<mx:DataGridColumn headerText="密码" dataField="userpwd"/>
<mx:DataGridColumn headerText="邮件" dataField="useremail"/>
<mx:DataGridColumn headerText="年龄" dataField="userage"/>
</mx:Array>
</mx:columns>
</mx:DataGrid>
</mx:Application>