1.引入核心包:
commons-logging-1.0.4,
freemarker-2.3.8,
ognl-2.6.11,
struts2-core-2.0.11.1,
xwork-2.0.4
2.web-xml添加描述文件:
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.编写Action文件继承ActionSupport
添加成员变量,getter和setter方法。编写execute()方法
4.配置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>
<!-- <constant name="struts.enable.DynamicMethodInvocation"-->
<!-- value="false" />-->
<!-- <constant name="struts.devMode" value="false" />-->
<package name="struts2" extends="struts-default">
<action name="loginAction" class="com.tyut.caiqm.action.LoginAction">
<result name="reload">/index.jsp</result>
<result >/suc.jsp</result>
</action>
<!-- <action name="....." class="....." method=".....">-->
<!--其中name可以随便起,这是以后自己要用到的,class就是类的全名,刚才说action不需要继承某个类,
也就是说他可以不遵循某中规范,可以在自定义的action中自定义处理方法-->
<!-- <result>........</result> 相当与actionforward-->
<!-- </action>-->
</package>
</struts>
5.编写页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>
<s:form action="loginAction">
<s:textfield name="username" label="用户名:"></s:textfield><br>
<s:password name="password" label="密码:"></s:password><br>