1.index.jsp
<s:form action="HelloChina.action" method="post">
Name: <s:textfield name="name" value=""/>
<s:submit />
</s:form>
2.HelloChina
package com.xushi.bll;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class HelloChina extends ActionSupport {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() {
HttpServletRequest request = ServletActionContext.getRequest();
request.getSession().setAttribute("abc", request.getParameter("name"));
//Map map=ActionContext.getContext().getSession();
// map.put("abc", request.getParameter("name"));
return "test";
}
}
3.test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>
<s:property value="#session.abc" />
</body>
</html>
4.struts.xml
<action name="HelloChina" class="com.xushi.bll.HelloChina">
<result name="test">/pack/test.jsp</result>
</action>