`
大连IT精英
  • 浏览: 3504 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

MiniHR

阅读更多
WebContent\
01.\employee\exception.jsp
02.\employee\index.jsp
03.\employee\search.jsp
04.\reports\menu.jsp
05.\WEB-INF\struts-config-employee.xml
06.\WEB-INF\struts-config-reports.xml
07.\WEB-INF\struts-config.xml
08.\WEB-INF\tiles-defs.xml
09.\WEB-INF\validation.xml
10.\WEB-INF\validator-rules.xml
11.\WEB-INF\web.xml
12.\footer.jsp
13.\header.jsp
14.\index.jsp
15.\mainLayout.jsp

src\
\com.struts1.minihr\
16.Employee.java
17.EmployeeSearchService.java
18.LoginAction.java
19.MiniHrValidator.java
20.NoResultsFoundException.java
21.SearchAction.java
22.SearchForm.java
23.MessagResources_es.properties
24.MessagResources.properties
\com.struts1.minihr.security\
25.SecurityService.java
26.SecurityServiceImpl.java
27.User.java

01.
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<html:errors/>


02.
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

<html>
<head>
<title>ABC, Inc. Human Resources Portal</title>
</head>

<body>
<font size="+1">ABC, Inc. Human Resources Portal</font>
<br>
<hr width="100%" noshade="true">
&#149; Add an Employee
<br>
&#149;
<html:link action="/viewSearch">Search for Employees</html:link>
<br>
</body>

</html>


03.
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>

<html>
<head>
<title>
<bean:message key="title.application"/> -
<bean:message key="title.employee.search"/>
</title>
</head>
<body>

<font size="+1">
<bean:message key="title.application"/> -
<bean:message key="title.employee.search"/>
</font><br>
<hr width="100%" noshade="true">

<html:errors/>

<html:form action="/search">

<table>
<tr>
<td align="right"><bean:message key="label.search.name"/>:</td>
<td><html:text property="name"/></td>
</tr>
<tr>
<td></td>
<td>-- or --</td>
</tr>
<tr>
<td align="right"><bean:message key="label.search.ssNum"/>:</td>
<td><html:text property="ssNum"/> (xxx-xx-xxxx)</td>
</tr>
<tr>
<td></td>
<td><html:submit><bean:message key="label.submit"/></html:submit></td>
</tr>
</table>

</html:form>

<logic:present name="searchForm" property="results">

<hr width="100%" size="1" noshade="true">

<bean:size id="size" name="searchForm" property="results"/>
<logic:equal name="size" value="0">
<center><font color="red"><b>
<bean:message key="error.search.not.found"/>
</b></font></center>
</logic:equal>

<logic:greaterThan name="size" value="0">
<table border="1">
<tr>
<th><bean:message key="label.search.name"/></th>
<th><bean:message key="label.search.ssNum"/></th>
</tr>
<logic:iterate id="result" name="searchForm" property="results">
<tr>
<td><bean:write name="result" property="name"/></td>
<td><bean:write name="result" property="ssNum"/></td>
</tr>
</logic:iterate>
</table>

</logic:greaterThan>

</logic:present>
</body>
<html>


04.
<html>
<head>
<title>ABC, Inc. Human Resources Portal - Reports Menu</title>
</head>
<body>

<font size="+1">
ABC, Inc. Human Resources Portal - Reports Menu
</font><br>
<hr width="100%" noshade="true">

&#149; New Employee Report<br>
&#149; 5-Year Employee Report<br>
&#149; 10-Year Employee Report<br>
&#149; 20-Year Employee Report<br>

</body>
<html>


05.
<?xml version="1.0"?>

<!DOCTYPE struts-config PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
  "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>

    <!-- Form Beans Configuration -->
    <form-beans>
        <form-bean name="searchForm" type="com.struts1.minihr.SearchForm" />
    </form-beans>

    <!-- Action Mappings Configuration -->
    <action-mappings>
        <action path="/viewSearch" forward=".search.page" />
        <action path="/search"
            type="com.struts1.minihr.SearchAction"
            name="searchForm"
            scope="request"
            validate="false"
            input=".search.page" >
            <exception type="com.struts1.minihr.NoResultsFoundException"
                key="error.search.not.found"
                path=".error.page" />
        </action>
    </action-mappings>

</struts-config>


06.
<?xml version="1.0"?>

<!DOCTYPE struts-config PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
  "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>

    <!-- Form Beans Configuration -->
    <form-beans>
        <form-bean name="searchForm" type="com.struts1.minihr.SearchForm" />
    </form-beans>

    <!-- Action Mappings Configuration -->
    <action-mappings>
        <action path="/viewSearch" forward=".search.page" />
        <action path="/search"
            type="com.struts1.minihr.SearchAction"
            name="searchForm"
            scope="request"
            validate="false"
            input=".search.page" >
            <exception type="com.struts1.minihr.NoResultsFoundException"
                key="error.search.not.found"
                path=".error.page" />
        </action>
    </action-mappings>

</struts-config>


07.
<?xml version="1.0"?>

<!DOCTYPE struts-config PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
  "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>

    <!-- Global Exception Configuration -->
    <global-exceptions>
<!--
        <exception type="com.struts1.minihr.NoResultsFoundException"
            key="error.NoResultsFoundException"
            path="/employee/exception1.jsp" />

        <exception type="com.struts1.minihr.NoResultsFoundException"
            key="error.NoResultsFoundException"
            path="error.page" />
-->
    </global-exceptions>

    <!-- Global Forwards Configuration -->
    <global-forwards>
        <forward name="viewSearch" path="/employee/viewSearch.do"/>
    </global-forwards>

    <!-- Message Resources Configuration -->
    <message-resources parameter="com.struts1.minihr.MessageResources" />

    <!-- Tiles Configuration -->
    <plug-in className="org.apache.struts.tiles.TilesPlugin">
        <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
    </plug-in>

    <!-- Validator Configuration -->
    <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,
               /WEB-INF/validation.xml" />
    </plug-in>


</struts-config>



08.
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 1.3//EN"
       "http://struts.apache.org/dtds/tiles-config_1_3.dtd">

<tiles-definitions>

    <!-- Main Layout -->
    <definition name=".main.layout" path="/mainLayout.jsp">
        <put name="title" value="" />
        <put name="header" value="/header.jsp" />
        <put name="body" value="" />
        <put name="footer" value="/footer.jsp" />
    </definition>

    <!-- Exception Page -->
    <definition name=".error.page" extends=".main.layout">
        <put name="title" value="ABC, Inc. Human Resources Portal - Employee Exception" />
        <put name="body" value="/employee/exception.jsp" />
    </definition>

    <!-- Search Page -->
    <definition name=".search.page" extends=".main.layout">
        <put name="title" value="ABC, Inc. Human Resources Portal - Employee Search" />
        <put name="body" value="/employee/search.jsp" />
    </definition>

    <!-- Menu Page -->
    <definition name=".menu.page" extends=".main.layout">
        <put name="title" value="ABC, Inc. Human Resources Portal - Employee Menu" />
        <put name="body" value="/reports/menu.jsp" />
    </definition>

</tiles-definitions>



09.
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE form-validation PUBLIC
     "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN"
     "http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">

<form-validation>
    <global>
        <!--
            An example global constant
            <constant>
                <constant-name>postalCode</constant-name>
                <constant-value>^\d{5}\d*$</constant-value>
            </constant>
            end example
        -->
    </global>
    <formset>
        <form name="searchForm">
            <field property="ssNum" depends="ssNum">
                <arg key="label.search.ssNum" />
                <var>
                    <var-name>mask</var-name>
                    <var-value>^\d{3}-d{2}-d{4}$</var-value>
                </var>
            </field>
            <!--
            <field property="ssNum" depends="mask">
                <arg key="label.search.ssNum" />
                <var>
                    <var-name>mask</var-name>
                    <var-value>^\d{3}-d{2}-d{4}$</var-value>
                </var>
            </field>
            -->
            <!--
            field property="ssNum" depends="required, ssNum">
                <arg key="label.search.ssNum" />
                <var>
                    <var-name>mask</var-name>
                    <var-value>^\d{3}-d{2}-d{4}$</var-value>
                </var>
            </field>
            -->
        </form>
    </formset>
</form-validation>


10.valdator-rules.xml(from struts 1.3.10)

11.
<?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/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <display-name>MiniHR</display-name>

    <!-- Action Servlet Configuration -->
    <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>config/employee</param-name>
            <param-value>/WEB-INF/struts-config-employee.xml</param-value>
        </init-param>
        <init-param>
            <param-name>config/reports</param-name>
            <param-value>/WEB-INF/struts-config-reports.xml</param-value>
        </init-param>
        <init-param>
            <param-name>chainConfig</param-name>
            <param-value>org/apache/struts/tiles/chain-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Action Servlet Mapping -->
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <!-- The Welcome File List -->
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>

    <!-- StrutsTag Library Descriptors
    <taglib>
        <tablib-uri>/WEB-INF/tlds/struts-bean.tld</tablib-uri>
        <tablib-location>/WEB-INF/tlds/struts-bean.tld</tablib-location>
    </taglib>
    <taglib>
        <tablib-uri>/WEB-INF/tlds/struts-html.tld</tablib-uri>
        <tablib-location>/WEB-INF/tlds/struts-html.tld</tablib-location>
    </taglib>
    <taglib>
        <tablib-uri>/WEB-INF/tlds/struts-logic.tld</tablib-uri>
        <tablib-location>/WEB-INF/tlds/struts-logic.tld</tablib-location>
    </taglib>
    -->

</web-app>


12.
<hr width="100%" noshade="true">
Copyright &copy; ABC, Inc.


13.
<font size="+1">ABC, Inc. Human Resources Portal</font>
<br>
<hr width="100%" noshade="true">


14.
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<html>
<head>
<title><bean:message key="title.application"/></title>
</head>
<body>

<font size="+1"><bean:message key="title.application"/></font><br>

<hr width="100%" noshade="true">

&#149; <bean:message key="link.employee.add"/><br>
&#149; <html:link forward="viewSearch"><bean:message key="link.employee.search"/></html:link><br>

&#149; <html:link action="/reports/viewMenu">Reports Menu</html:link><br>

</body>
</html>



15.
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>

<html>
<head>
<title><tiles:getAsString name="title" /></title>
</head>
<body>
<tiles:insert attribute="header" />
<tiles:insert attribute="body" />
<tiles:insert attribute="footer" />
</body>
</html>


16.
package com.struts1.minihr;

public class Employee {

    private String name;
    private String ssNum;

    public Employee(String name, String ssNum) {
        this.name = name;
        this.ssNum = ssNum;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSsNum() {
        return ssNum;
    }

    public void setSsNum(String ssNum) {
        this.ssNum = ssNum;
    }


}


17.
package com.struts1.minihr;

import java.util.ArrayList;

public class EmployeeSearchService {

    /* Hard-coded sample data. Normally this would come from a real data source such as a database. */
    private static Employee[] employees =
    {
        new Employee("Bob Davidson", "123-45-6789"),
        new Employee("Mary Williams", "987-65-4321"),
        new Employee("Jim Smith", "111-11-1111"),
        new Employee("Beverly Harris", "222-22-2222"),
        new Employee("Thomas Frank", "333-33-3333"),
        new Employee("Jim Davidson", "444-44-4444")
    };

    // Search for employees by name.
    public ArrayList searchByName(String name) {
        ArrayList resultList = new ArrayList();

        for (int i=0; i < employees.length; i++) {
            if (employees[i].getName().toUpperCase().indexOf(name.toUpperCase())!= -1) {
                resultList.add(employees[i]);
            }
        }

        return resultList;
    }

    // Search for employee by social security number.
    public ArrayList searchBySsNum(String ssNum) {
        ArrayList resultList = new ArrayList();

        for (int i=0; i < employees.length; i++) {
            if (employees[i].getSsNum().equals(ssNum)) {
                resultList.add(employees[i]);
            }
        }

        return resultList;
    }
}


18.
package com.struts1.minihr;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.struts1.minihr.security.SecurityService;
import com.struts1.minihr.security.SecurityServiceImpl;
import com.struts1.minihr.security.User;

public final class LoginAction extends Action {

    public ActionForward execute(
            ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
    throws Exception {
        HttpSession session = request.getSession();
        String username = (String) PropertyUtils.getSimpleProperty(form, "username");
        String password = (String) PropertyUtils.getSimpleProperty(form, "password");
        SecurityService service = new SecurityServiceImpl();
        User user = service.authenticate(username, password);
        session.setAttribute("User", user);
        // Forward control to this Action's success forward
        return mapping.findForward("success");
    }
}


19.
package com.struts1.minihr;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.validator.Field;
import org.apache.commons.validator.Validator;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.util.ValidatorUtils;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.Resources;

public class MiniHrValidator {

    public static boolean validateSsNum (Object bean,
            ValidatorAction action,
            Field field,
            ActionMessages errors,
            Validator validator,
            HttpServletRequest request) {

        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

        if (value == null || value.length() < 11) {
            errors.add(field.getKey(),
                    Resources.getActionMessage(request, action, field));
            return false;
        }

        for (int i = 0; i < 11; i++) {
            if (i == 3 || i == 6) {
                if (value.charAt(i) != '-') {
                    errors.add(field.getKey(),
                            Resources.getActionMessage(request, action, field));
                    return false;
                }
            } else if ("0123456789".indexOf(value.charAt(i)) == -1) {
                errors.add(field.getKey(),
                        Resources.getActionMessage(request, action, field));
                return false;
            }
        }

        return true;
    }
}


20.
package com.struts1.minihr;

public class NoResultsFoundException extends Exception {

}


21.
package com.struts1.minihr;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class SearchAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

	    EmployeeSearchService service = new EmployeeSearchService();
	    ArrayList results;

	    SearchForm searchForm = (SearchForm) form;

	    // Perform employee search based on the criteria entered.
	    String name = searchForm.getName();
	    if (name != null && name.trim().length() > 0) {
	      results = service.searchByName(name);
	    } else {
	      results = service.searchBySsNum(searchForm.getSsNum().trim());
	    }

	    // Throw an application exception if results were not found.
	    if (results.size() < 1) {
	      throw new NoResultsFoundException();
	    }

	    // Place search results in SearchForm for access by JSP.
	    searchForm.setResults(results);

	    // Forward control to this Action's input page.
	    return mapping.getInputForward();

	}

}


22.
package com.struts1.minihr;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.validator.ValidatorForm;

public class SearchForm extends ValidatorForm {

    private static final long serialVersionUID = 1L;

    private String name = null;
    private String ssNum = null;
    private List results = null;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSsNum() {
        return ssNum;
    }

    public void setSsNum(String ssNum) {
        this.ssNum = ssNum;
    }

    public List getResults() {
        return results;
    }

    public void setResults(List results) {
        this.results = results;
    }

//    @Override
//    // Reset form fields.
//    public void reset(ActionMapping mapping, HttpServletRequest request) {
//        this.name = null;
//        this.ssNum = null;
//        this.results = null;
//    }
//
//    @Override
//    // Validate form data.
//    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
//        ActionErrors errors = new ActionErrors();
//
//        boolean nameEntered = false;
//        boolean ssNumEntered = false;
//
//        // Determine if name has been entered.
//        if (name != null && name.length() > 0) {
//            nameEntered = true;
//        }
//
//        // Determine if social security number has been entered.
//        if (ssNum != null & ssNum.length() > 0) {
//            ssNumEntered = true;
//        }
//
//        // Validate that either name or social security number has been entered.
//        if (!nameEntered && !ssNumEntered) {
//            errors.add(null, new ActionMessage("error.search.criteria.missing"));
//        }
//
//        // Validate format of social security number if it has been entered.
//        if (ssNumEntered && !isValidSsNum(ssNum.trim())) {
//            errors.add("ssNum",
//                    new ActionMessage("error.search.ssNum.invalid"));
//        }
//
//        return errors;
//    }
//
//    // Validate format of social security number.
//    private static boolean isValidSsNum(String ssNum) {
//        if (ssNum.length() < 11) {
//            return false;
//        }
//
//        for (int i = 0; i < 11; i++) {
//            if (i == 3 || i == 6) {
//                if (ssNum.charAt(i) != '-') {
//                    return false;
//                }
//            } else if ("0123456789".indexOf(ssNum.charAt(i)) == -1) {
//                return false;
//            }
//        }
//
//        return true;
//    }
}


23.
# Title Resources
title.application=ABC, Inc. Recursos Humanos Porta
title.employee.search=Búsqueda Del Empleado

# Link Resources
link.employee.add=Agregue a empleado
link.employee.search=Búsqueda para los empleados

# Label Resources
label.search.name=Nombre
label.search.ssNum=Número De la Seguridad Social
label.submit=Someter

# Error Resources
error.search.criteria.missing=El Faltar De los Criterios De la Búsqueda
error.search.ssNum.invalid=Número Inválido De la Seguridad Social
error.search.not.found=Ningunos Empleados Encontraron
errors.header=<font color="red"><cTypeface:Bold>Error De la Validación(s)</b></font><ul>
errors.footer=</ul><hr width="100%" size="1" noshade="true">
errors.suffix=<li>
errors.prefix=</li>


24.
# Title Resources
title.application=ABC, Inc. Human Resources Portal
title.employee.search=Employee Search

# Link Resources
link.employee.add=Add an Employee
link.employee.search=Search for Employees

# Label Resources
label.search.name=Name
label.search.ssNum=Social Security Number
label.submit=Submit

# Error Resources
error.search.criteria.missing=Search Criteria Missing
error.search.ssNum.invalid=Invalid Social Security Number
error.search.not.found=No Employees Found
errors.header=<font color="red"><cTypeface:Bold>Validation Error(s)</b></font><ul>
errors.footer=</ul><hr width="100%" size="1" noshade="true">
errors.prefix=<li>
errors.suffix=</li>



25.
package com.struts1.minihr.security;

import javax.security.sasl.AuthenticationException;

public interface SecurityService {
  public User authenticate(String username, String password)
    throws AuthenticationException;
}


26.
package com.struts1.minihr.security;

import java.util.HashMap;
import java.util.Map;

import javax.security.sasl.AuthenticationException;

public class SecurityServiceImpl implements SecurityService {
  private Map users;
  private static final String ADMIN_ROLE = "administrator";

  public SecurityServiceImpl() {
    users = new HashMap();
    users.put("bsiggelkow",
      new User( "bsiggelkow","Bill", "Siggelkow",  "thatsme",
        new String[] {ADMIN_ROLE}));
    users.put("jholmes",
      new User( "jholmes","James", "Holmes",  "maindude",
        new String[] {ADMIN_ROLE}));
    users.put("gburdell",
      new User( "gburdell","George", "Burdell",  "gotech",
        new String[] {ADMIN_ROLE}));
  }

  public User authenticate(String username, String password)
    throws AuthenticationException
  {
    User user = (User) users.get(username);
    if (user == null)
      throw new AuthenticationException("Unknown user");
    boolean passwordIsValid = user.passwordMatch(password);
    if (!passwordIsValid)
      throw new AuthenticationException("Invalid password");
    return user;
  }
}


27.
package com.struts1.minihr.security;

import java.io.Serializable;

public class User implements Serializable {
  private String firstName;
  private String lastName;
  private String username;
  private String password;
  private String[] roles;

  public User(String name, String fName, String lName,
    String pwd, String[] assignedRoles) {
    username = name;
    firstName = fName;
    lastName  = lName;
    password  = pwd;
    roles = assignedRoles;
  }

  public String getUsername() {
    return username;
  }

  public String getFirstName() {
    return firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public boolean passwordMatch(String pwd) {
    return password.equals(pwd);
  }

  public boolean hasRole(String role) {
    if (roles.length > 0) {
      for (int i=0; i<roles.length; i++) {
        if (role.equals(roles[i])) return true;
      }
    }
    return false;
  }

  public boolean isAdministrator() {
    return hasRole("administrator");
  }
}

分享到:
评论
1 楼 bsn_007 2013-11-26  
what is password

相关推荐

Global site tag (gtag.js) - Google Analytics