<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.i18n.encoding" value="utf-8"></constant> <package name="tag" namespace="/tag" extends="struts-default"> <action name="tagAction" class="action.TagAction"> <result name="tag" type="dispatcher">/tag.jsp</result> <result name="input" type="dispatcher">/index.jsp</result> </action> </package> </struts>
package action; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.interceptor.ServletRequestAware; import com.opensymphony.xwork2.ActionSupport; import entity.User; public class TagAction extends ActionSupport implements ServletRequestAware{ private HttpServletRequest request; private User user; public String init(){ Map<String,String> roles = new HashMap<String,String>(); roles.put("程序员", "程序员"); roles.put("分析师", "分析师"); roles.put("架构师", "架构师"); Map<String,String> methods = new HashMap<String,String>(); methods.put("init", "初始化"); methods.put("register", "注册"); methods.put("dispatcher", "转发"); request.setAttribute("roles", roles); request.setAttribute("methods", methods); return INPUT; } public String register(){ System.out.println("username = [ "+user.getUsername()+" ]"); System.out.println("password = [ "+user.getPassword()+" ]" ); System.out.println("role = [ "+user.getStatus()+" ]"); for(int i =0; i< user.getRoles().length; i++){ System.out.println("role["+i+"] = "+user.getRoles()[i]); } return INPUT; } public String dispatcher(){ return "tag"; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public void setServletRequest(HttpServletRequest request) { this.request = request; } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <% 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"> --> <script type="text/javascript"> function changeMethod(){ var method = document.getElementsByName("invoke")[0].value; document.forms[0].action = "tag/tagAction!"+method; } window.onload = changeMethod; </script> </head> <body> <s:form action="tag/tagAction!register"> <s:textfield name="user.username" label="用户名"></s:textfield><br/> <s:password name="user.password" label="密 码"></s:password><br/> <s:checkbox label="管理员" name="user.status"></s:checkbox> <s:checkboxlist list="#request.roles" name="user.roles" label="职业选择"></s:checkboxlist> <s:radio list="#request.methods" label="选择执行方法" name="invoke" onclick="changeMethod()"></s:radio> <s:submit value="提交"></s:submit> </s:form> <s:debug></s:debug> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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 'tag.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> This is my JSP page. <br> </body> </html>