以下是代码演示:
index:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@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">
-->
</head>
<body>
<form action="testManager" method="post">
<input type="text" name="userInfo.usName">
<input type="text" name="userInfo.usAge">
<input type="text" name="userInfo.usSex">
<input type="submit" value="提交"/>
</form>
</body>
</html>
MyJsp.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@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 'MyJsp.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="userInfo.usName"/> 年龄:<s:property value="userInfo.usAge"/> 性别:<s:property value="userInfo.usSex"/>
</body>
</html>
com.po UserInfo:
package com.zuxia.yc42.po;
import java.io.Serializable;
public class UserInfo implements Serializable
{
private String usName;
private Integer usAge;
private String usSex;
public String query()
{
return null;
}
public String getUsName() {
return usName;
}
public void setUsName(String usName) {
this.usName = usName;
}
public Integer getUsAge() {
return usAge;
}
public void setUsAge(Integer usAge) {
this.usAge = usAge;
}
public String getUsSex() {
return usSex;
}
public void setUsSex(String usSex) {
this.usSex = usSex;
}
}
com.action MyTestAction:
package com.zuxia.yc42.action;
import com.zuxia.yc42.po.UserInfo;
public class MyTestAction
{
private UserInfo userInfo;
public String init()
{
System.out.println("----------");
System.out.println(userInfo.getUsName()+"\t"+userInfo.getUsAge()+"\t"+userInfo.getUsSex());
return "ok";
}
public UserInfo getUserInfo() {
return userInfo;
}
public void setUserInfo(UserInfo userInfo) {
this.userInfo = userInfo;
}
}
struts.xml:
<?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>
<package name="TestManager" extends="struts-default">
<action name="testManager" class="com.zuxia.yc42.action.MyTestAction" method="init">
<result name="ok">/MyJsp.jsp</result>
<result name="error">/index.jsp</result>
</action>
<action name="ts1"></action>
<action name="ts2"></action>
</package>
</struts>
web.xml:
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></web-app>