准备阶段
首先需要导入struts2.3.1的9个架包
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang-2.5.jar
commons-logging-1.1.1.jar
freemarker-2.3.16.jar
javassist-3.11.0.GA.jar
ognl-3.0.1.jar
struts2-core-2.2.3.1.jar
xwork-core-2.2.3.1.jar
登录界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'test.jsp' starting page</title>
<h1>用分号分割用户名和密码</h1>
</head>
<body>
<form action="userAction.action">
User Info:<input type="text" name="user"/> <br>
<input type="submit" value="submit"/>
</form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>Struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default" >
<action name="userAction" class="action.UserAction">
<result name="success">/jsp/output.jsp</result>
</action>
</package>
</struts>
action包
package action;
import com.opensymphony.xwork2.ActionSupport;
import bean.User;
public class UserAction extends ActionSupport {
private User user;
public void setUser(User user) {
this.user = user;
}
public User getUser() {
return user;
}
@Override
public String execute() throws Exception
{
// TODO Auto-generated method stub
System.out.println("UserName:"+user.getUsername());
System.out.println("Password:"+user.getPassword());
return SUCCESS;
}
}
converter包
package converter;
import java.util.Map;
import java.util.StringTokenizer;
import bean.User;
import ognl.DefaultTypeConverter;
//类型转换器converter 继承ognl包下DefaultTypeConverter默认的类型转换器
public class UserConverter extends DefaultTypeConverter {
@Override
public Object convertValue(Map context, Object value, Class toType) {
// TODO Auto-generated method stub
//判断页面是否提供数据,有数据则执行if,否则return null。
if(User.class==toType) // 从页面向后台对象转换
{
//以数组形式接受字符串
String[] str =(String[])value;
String firstValue = str[0];
//StringTonkenizer类用于获取页面字符串和分隔符
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;
}
}
建立关联UserAction-conversion.properties
user=converter.UserConverter
bean包
package bean;
public class User {
private String username;
private String password;
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
}
返回页面output.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!--taglib标签库-->
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'output.jsp' starting page</title>
</head>
<body>
<!--获得 再转到此页面属性之前action对象-->
<s:property value="user" default="user"/>
</body>
</html>