1.jsp登陆界面
<%@ 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 'Login2_1.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> 提示:姓名和密码用冒号分开<br/> <form action="userAction.action"> User : <input type="text" name="user"/><br> <input type="submit" value="提交"/><br/> </form> </body> </html>
2.Strtus配置Struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="struts2" extends="struts-default"> <action name="login" class="Struts2.WildCat.com.LoginAction" method="myExecute"> <result name="success">/result.jsp</result> </action> <action name="userAction" class="Struts2.WildCat.com.UserAction"> <result name="success">/result2_2.jsp</result> </action> <action name="userAction3" class="Struts2.WildCat.com.UserAction3"> <result name="success">/result3_1.jsp</result> </action> <action name="registerAction" class="Struts2.WildCat.com.RegisterAction"> <result name="success" >/Register_Result.jsp</result> </action> </package> </struts>
3.User类
package com.WildCat.bean; public class User { 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; } }
4.类型转换配置文件UserAction-conversion.properties
user=com.WildCat.converter.UserConverter
5.类型转换器
package com.WildCat.converter; import java.util.Map; import java.util.StringTokenizer; import com.WildCat.bean.User; import ognl.DefaultTypeConverter; public class UserConverter extends DefaultTypeConverter { @Override public Object convertValue(Map context, Object value, Class toType) { if (User.class==toType)//从页面向后台转换 { String[] str=(String[])value; String firstValue=str[0]; StringTokenizer st=new StringTokenizer(firstValue,":"); String username=st.nextToken(); String password=st.nextToken(); User user=new User(); user.setUsername(username); user.setPassword(password); return user; } else if (String.class==toType)//从后台对象向前台转换 { User user=(User)value; String username=user.getUsername(); String password=user.getPassword(); String userInfo="UserName: "+username+" PassWord: "+password; return userInfo; } return null; } }
6.Action
package Struts2.WildCat.com; import com.WildCat.bean.User; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String execute() throws Exception { System.out.println("UserName: "+user.getUsername()); System.out.println("PassWord: "+user.getPassword()); return SUCCESS; } }
7.结果界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <% 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 'result2_2.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> <s:property value="user"/> </body> </html>