【功能概述】
1. 搭建Struts2应用框架, 实现由UI层向控制器的数据传递;
2. 手工加载Struts2的jar包;
【配置说明】
IDE: MyEclipse 6.0
DB: Oracle 10g
Struts version: Struts 2.1.6
Struts Library: < struts2-core-2.1.6.jar
xwork-2.1.2.jar
ognl-2.6.11.jar
freemarker-2.3.13.jar
commons-logging-api-1.1.jar
commons-fileupload-1.2.1.jar >
【源代码】
1. UI层:
2. web.xml
3. Struts2核心配置文件: struts.xml
4. 控制器:
1. 搭建Struts2应用框架, 实现由UI层向控制器的数据传递;
2. 手工加载Struts2的jar包;
【配置说明】
IDE: MyEclipse 6.0
DB: Oracle 10g
Struts version: Struts 2.1.6
Struts Library: < struts2-core-2.1.6.jar
xwork-2.1.2.jar
ognl-2.6.11.jar
freemarker-2.3.13.jar
commons-logging-api-1.1.jar
commons-fileupload-1.2.1.jar >
【源代码】
1. UI层:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>Add Employee</title>
</head>
<body>
<!-- 注意: .action后缀不写亦可 -->
<form action="EmployeeAction.action" method="post">
<input type="text" name="empno"><br>
<input type="text" name="ename"><br>
<input type="text" name="hiredate"><br>
<input type="submit" value="submit">
</form>
</body>
</html>
2. 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">
<!-- Struts2 Configuration -->
<filter>
<filter-name>FilterDispatcher</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterDispatcher</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3. Struts2核心配置文件: 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>
<!-- 注意: name属性是必须的, 值可以任意 -->
<!-- extends属性非必须, 但如果不写的话, 无法将UI层的输入值注入到控制器属性中. -->
<package name="default" extends="struts-default">
<!-- result的配置不是必须的(此处没有配置result) -->
<action name="EmployeeAction" class="com.howin.action.EmployeeAction"/>
</package>
</struts>
4. 控制器:
package com.howin.action;
import com.opensymphony.xwork2.Action;
//★注意: Struts2控制器可以不实现任何接口
//也可实现Action接口
//或者继承ActionSupport(输入校验使用)
public class EmployeeAction
{
//★注意: 属性名必须与UI层页面输入标签的name保持一致
private String empno;
private String ename;
private String hiredate;
//注意:属性的setter方法是必须的, 否则无法将UI层输入的值注入到属性中
public void setEmpno(String empno)
{
this.empno = empno;
}
public void setEname(String ename)
{
this.ename = ename;
}
public void setHiredate(String hiredate)
{
this.hiredate = hiredate;
}
public String execute()
{
System.out.println(empno+ename+hiredate);
return null;
}
}