//MyServlet的代码
package cn.itcast.actions;
import javax.servlet.ServletException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.struts.action.ActionServlet;
import cn.itcast.enums.SexEnum;
public class MyActionServlet extends ActionServlet {
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
//这是注册转换器
ConvertUtils.register(new SexConvert(), SexEnum.class);
}
}
//这是registAction的代码
package cn.itcast.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import cn.itcast.enums.SexEnum;
import cn.itcast.form.SexForm;
public class RegistAction extends DispatchAction {
public ActionForward go(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setAttribute("sex", SexEnum.values());
return mapping.findForward("regist");
}
public ActionForward regist(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
SexForm sexForm = (SexForm)form;
System.out.println(sexForm.getSex().name());
return mapping.findForward("success");
}
}
//这是form的代码
package cn.itcast.form;
import org.apache.struts.action.ActionForm;
import cn.itcast.enums.SexEnum;
public class SexForm extends ActionForm {
private SexEnum sex;
public SexEnum getSex() {
return sex;
}
public void setSex(SexEnum sex) {
this.sex = sex;
}
}
//这是convert的代码
package cn.itcast.actions;
import org.apache.commons.beanutils.Converter;
import com.sun.rmi.rmid.ExecOptionPermission;
import cn.itcast.enums.SexEnum;
public class SexConvert implements Converter {
public Object convert(Class arg0, Object arg1) {
//这是将得到的String类型转换成Enum类型
return SexEnum.valueOf((String)arg1);
}
}
//这是enum的代码
package cn.itcast.enums;
public enum SexEnum {
MAN{
public String getTitle(){
return "男";
}
},
WOMAN{
public String getTitle(){
return "女";
}
},
BOTH{
public String getTitle(){
return "男女均可";
}
};
//这句代码千万不能少
public abstract String getTitle() ;
public String getValue(){
//这是 返回此枚举常量的名称,如:MAN,WOMAN,BOTH
return this.name();
}
}
///这是web.xml的代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>cn.itcast.actions.MyActionServlet</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>
//这是struts-config的代码
<?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="sexForm" type="cn.itcast.form.SexForm"></form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/test" type="cn.itcast.actions.RegistAction" parameter="p" scope="session">
<forward name="regist" path="/regist.jsp"></forward>
</action>
<action path="/regist" type="cn.itcast.actions.RegistAction" name="sexForm" parameter="p">
<forward name="success" path="/test.do?p=go" ></forward>
</action>
</action-mappings>
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>
//这是index.jsp的代码
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="${pageContext.request.contextPath}/test.do?p=go">click</a>
</body>
</html>
//这是regist.jsp的代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'regist.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="${pageContext.request.contextPath}/regist.do?p=regist" method="post">
<select name="sex">
<c:forEach items="${sex}" var="s">
<!-- 这里的title,value属性属性石SexEnum的title、value属性 -->
<option value="${s.value}" ${s.value == sexForm.sex.value ? 'selected = "selected"':"" } }'>${s.title}</option>
</c:forEach>
</select>
<input type="submit" value="提交"/>
</form>
</body>
</html>