声明:本帖非原创,内容来自网络。
0 项目工程结构图
1 引入的类jar包,如下图:
2 web.xml 配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems,Inc.//DTD Web Application 2.2//EN' 'http://java.sun.com/j2ee/dtds/web-app_2_2.dtd'>
<web-app>
<display-name>Struts Demo</display-name>
<!-- standard 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>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/userLogin.jsp</welcome-file>
</welcome-file-list>
<!-- Struts Tag Library Descripters -->
<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>
</web-app>
3 jsp
<%@ page language="java"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<html>
<head>
<title>JSP for userLoginForm form</title>
</head>
<body>
<html:form action="/userLogin">
userName : <html:text property="userName"/><html:errors property="userName"/><br/>
passWord : <html:password property="passWord"/><html:errors property="passWord"/><br/>
<html:submit/><html:reset/>
</html:form>
</body>
</html>
4 Action
UserLoginAction:
package com.yang.struts.demo;
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 UserLoginAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
UserLoginForm userLoginForm = (UserLoginForm) form;
// TODO Auto-generated method stub
if(userLoginForm.getUserName().equals("EricHe")
&& userLoginForm.getPassWord().endsWith("123456")){
request.setAttribute("userName",userLoginForm.getUserName());
return mapping.findForward("success");
}
return mapping.findForward("failure");
}
}
UserLoginForm.java
package com.yang.struts.demo;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class UserLoginForm extends ActionForm {
private String userName;
private String passWord;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
/**
* 重置属性
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.userName = null;
}
/**
* 验证属性非空,返回ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if ((userName == null) || (userName.length() < 1))
errors
.add("username", new ActionMessage(
"username is null"));
return errors;
}
}
5 struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="userLoginForm" type="com.yang.struts.demo.UserLoginForm" />
</form-beans>
<global-exceptions />
<global-forwards >
</global-forwards>
<action-mappings>
<action
attribute="userLoginForm"
input="/userLogin.jsp"
name="userLoginForm"
path="/userLogin"
scope="request"
type="com.yang.struts.demo.UserLoginAction"
validate="true">
<forward name="success" path="/userLoginSuccess.jsp" />
<forward name="failure" path="/tryAgin.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.yang.struts.demo.ApplicationResources" />
</struts-config>
6 运行
启动Tomcat,输入地址,运行。