现在看看目录结构:
其中my下有2个包,一个是
ExampleAction.java
package my;
import com.opensymphony.xwork2.ActionSupport;
public class ExampleAction extends ActionSupport{
int id;
String name;
String phone;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String execute() throws Exception {
name="IT1995";
phone="124535458482";
return "success";
}
}
一个是HelloAction.java
package my;
import org.apache.log4j.Logger;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport{
static Logger logger=Logger.getLogger(HelloAction.class);
//input参数
int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
String name;
@Override
public String execute() throws Exception {
logger.debug("Got: id="+id+", name= "+name);
return "success";
}
}
log4.properties配置如下:
# - change the package prefix 'my' to your package prefix, such as 'com.xxx'
# - change threshold level to TRACE,DEBUG,INFO,WARN,ERROR,FATAL
# C,R is name of the following appenders
log4j.logger.my=DEBUG,C,R
log4j.appender.C=org.apache.log4j.ConsoleAppender
# Set the appender threshold to INFO
log4j.appender.C.Threshold=DEBUG
log4j.appender.C.layout=org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern=%-d{HH:mm:ss} %-5p %c %x - %m%n
# logging to file, rolling
# - change the log file's name and path, located in tomcat path
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.Threshold=DEBUG
log4j.appender.R.File=../mylogs/myweb.log
log4j.appender.R.MaxFileSize=256KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss} %-5p %c %x - %m%n
<?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="main" extends="struts-default">
<action name="Example" class="my.ExampleAction">
<result name="success"> /Result_Example.jsp</result>
</action>
<action name="Hello" class="my.HelloAction" >
<result name="success"> /Hello_success.jsp</result>
</action>
</package>
</struts>
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="main" extends="struts-default">
<action name="Example" class="my.ExampleAction">
<result name="success"> /Result_Example.jsp</result>
</action>
<action name="Hello" class="my.HelloAction" >
<result name="success"> /Hello_success.jsp</result>
</action>
</package>
</struts>
Hello_success.jsp配置如下:
<%@ 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 'Hello_success.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>
保存成功!
</body>
</html>
Hello.jsp配置如下:
<%@ 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 'Hello.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 method="post" action="Hello.action">
学号<input type="text" name="id"><br><br>
姓名<input type="text" name="name"><br><br>
<input type="submit" value="保存">
</form>
</body>
</html>
index.jsp就不说了,都是MyEclipse默认的
下面是Result_Example.jsp
<%@ 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 'Result_Example.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>
ID:<s:property value="id"/><br><br>
姓名:<s:property value="name"/><br><br>
手机号:<s:property value="phone"/><br><br>
</body>
</html>