- 将struts包中lib目录下的*.jar文件拷入WEB-INF/lib目录下
- 将struts包中lib目录下的*.tld文件拷入WEB-INF/tags目录下
- 修改web.xml文件 注意tag标签是在jsp-config下的
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
<init-param>
<param-name>detail</param-name>
<param-value>2</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>
<jsp-config>
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/tags/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/tags/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/tags/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>/WEB-INF/tags/struts-nested.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
- 新建actionform cn.hcs.form.UserForm文件
package cn.hcs.form;
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 UserForm extends ActionForm {
private String username;
private String password;
private String passwordcheck;
private String age;
private String school;
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
System.out.println("-----调用了UserForm.reset方法-");
this.setUsername("");
this.setPassword("");
this.setPasswordcheck("");
this.setAge("");
this.setSchool("");
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
ActionErrors actionErrors = new ActionErrors();;
if(this.getUsername()== null || "".equals(this.getUsername().trim())){
actionErrors.add("username", new ActionMessage("username.problem"));
}
return actionErrors;
}
- 新建action cn.hcs.form.UserAction文件
package cn.hcs.action;
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;
import cn.hcs.form.UserForm;
public class UserAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
UserForm userForm = (UserForm)form;
System.out.println("action username=" + userForm.getUsername());
return mapping.findForward("success");
}
}
- 在WEB-INF目录下新建一个struts-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="userform" type="cn.hcs.form.UserForm"></form-bean>
</form-beans>
<action-mappings>
<action path="/adduser"
name="userform"
input="/adduser.jsp"
type="cn.hcs.action.UserAction">
<forward name="success" path="/WEB-INF/pages/success.jsp" />
</action>
</action-mappings>
<message-resources parameter="cn.struts.messageresources.MessageResources"/>
</struts-config>
- 多语言的MessageResources 注意是在struts-config.xml文件中注明引用
在WEB目录下新建一个adduser.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/tags/struts-html.tld" prefix="html"%>
<%@taglib uri="/WEB-INF/tags/struts-bean.tld" prefix="bean"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><bean:message key="userform.title" />
</title>
</head>
<body>
<h1>
<bean:message key="userform.title" />
</h1>
<hr />
<html:errors />
<table>
<html:form action="adduser">
<tr>
<td>
<bean:message key="userform.username" />
</td>
<td>
<html:text property="username" />
</td>
</tr>
再在WEB-INF/pages目录下新建一个success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@
taglib uri="/WEB-INF/tags/struts-bean.tld"
prefix="bean"%>
<%@
taglib uri="/WEB-INF/tags/struts-html.tld"
prefix="html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<bean:message key="success"/>
</body>
</html>