一个简单的Struts例子

Java 发表评论

Dick Winters 于 2004年9月27日 晚

coffee.gifweb.xml

<?xml version=”1.0″ encoding=”ISO-8859-1″?>

<web-app xmlns=”http://java.sun.com/xml/ns/j2ee
    xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance
    xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd
    version=”2.4″>

<!– JSPC servlet mappings start –>

  <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>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <taglib>
    <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
  </taglib>

<!– JSPC servlet mappings end –>

</web-app>

coffee.gifstruts-config.xml

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<!DOCTYPE struts-config PUBLIC
“-//Apache Software Foundation//DTD Struts Configuration 1.2//EN”
http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd“>
<struts-config>


 <form-beans>
  <form-bean name=”userInfoForm” type=”dick.UserInfoForm” />
 </form-beans>


 <action-mappings>
  <action input=”/index.jsp” name=”userInfoForm” path=”/loginAction” scope=”session”
 type=”dick.LoginAction” validate=”false”>
   <forward name=”success” path=”/main.jsp” />     
  </action>
 </action-mappings>


 <message-resources parameter=”ApplicationResources” />


</struts-config>


coffee.gifindex.jsp

<%@ page contentType=”text/html; charset=UTF-8″ %>
<%@ taglib uri=”/WEB-INF/struts-bean.tld” prefix=”bean” %>
<%@ taglib uri=”/WEB-INF/struts-html.tld” prefix=”html” %>
<HTML>
<HEAD>
<TITLE><bean:message key=”login.jsp.title”/></TITLE>
<html:base/>
</HEAD>
<BODY>
<h3><bean:message key=”login.jsp.page.heading”/></h3>
<html:errors/>
<html:form action=”/loginAction.do” focus=”username”>
<TABLE border=”0″ width=”100%”>
<TR>
<TH align=”right”><bean:message key=”login.jsp.prompt.username”/></TH>
<TD align=”left”><html:text property=”username”/></TD>
</TR>
<TR>
<TH align=”right”><bean:message key=”login.jsp.prompt.password”/></TH>
<TD align=”left”><html:password property=”password”/></TD>
</TR>
<TR>
<TD align=”right”>
  <html:submit><bean:message key=”login.jsp.prompt.submit”/></html:submit>
</TD>
<TD align=”left”>
  <html:reset><bean:message key=”login.jsp.prompt.reset”/></html:reset>
</TD>
</TR>
</TABLE>
</html:form>
</BODY>
</HTML>

coffee.gifmain.jsp

<%@ page contentType=”text/html; charset=UTF-8″ %>
<%@ taglib uri=”/WEB-INF/struts-bean.tld” prefix=”bean” %>
<%@ taglib uri=”/WEB-INF/struts-logic.tld” prefix=”logic” %>

<HTML>
<HEAD>
<TITLE><bean:message key=”main.jsp.title”/></TITLE>
<html:base/>
</HEAD>
<BODY>
<logic:present name=”userInfoForm”>
<H3>
  <bean:message key=”main.jsp.welcome”/>
  <bean:write name=”userInfoForm” property=”username”/>!
</H3>
</logic:present>
</BODY>
</HTML>

coffee.gifUserInfoForm.java

package dick;
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 password;

  public String getUsername() {
    return (this.username);
  }
  public void setUsername(String username) {
    this.username = username;
  }

  public String getPassword() {
    return (this.password);
  }
  public void setPassword(String password) {
    this.password = password;
  }
}

coffee.gifLoginAction.java

package dick;
import java.io.IOException;
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;


public final class LoginAction 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.getPassword().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);
    }
   
    //调用业务逻辑
    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);         
         return mapping.findForward(“success”);
        }
      }
     
      catch(Throwable e){
        //处理可能出现的错误
        e.printStackTrace();
        ActionError error=new ActionError(e.getMessage());
        errors.add(ActionErrors.GLOBAL_ERROR,error);
      }
    } 
    //如出错就转向输入页面,并显示相应的错误信息
    saveErrors(request, errors);   
    return new ActionForward(mapping.getInput());   
  }
}


coffee.gifUserInfoBo.java

package dick;

public class UserInfoBo {

  public UserInfoBo(){
   
  }   

  public String validatePwd(String username,String password){
     
    String validateResult=””;
      
    if(username.equals(“Dick”)&&password.equals(“101”)){
      validateResult=”match”;
    }
    else{
     
      throw new RuntimeException(“error.noMatch”);
    }     
   
    return validateResult;  
   
  }
}

发表回复

您的电子邮箱地址不会被公开。

昵称 *