1、在Tomcat的webapps目录下新建一个文件夹,取名为Test。
2、在Test目录下新建一个WEB-INF目录。
3、在WEB-INF目录下建立classes和lib目录。classes目录用于存放应用需要加载的java类文件,lib目录存放Struts2包和其他需要用到的第三方库文件。
4、将Struts2的核心类库拷贝到lib目录下,分别是commons-logging-1.0.4.jar,freemarker-2.3.8.jar,ognl-2.6.11.jar,struts2-core-2.0.14.jar,xwork-2.0.7.jar。
5、在Test\WEB-INF目录下建立web.xml文件:
- <?xml version="1.0" encoding="GBK"?>
- <web-app version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <filter>
- <!-- 定义核心Filter的名字 -->
- <filter-name>struts2</filter-name>
- <!-- 定义核心Filter的实现类 -->
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
- </filter>
- <!-- 用FilterDispatcher来初始化Struts2并且处理所有的WEB请求 -->
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- </web-app>
<?xml version="1.0" encoding="GBK"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter> <!-- 定义核心Filter的名字 --> <filter-name>struts2</filter-name> <!-- 定义核心Filter的实现类 --> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <!-- 用FilterDispatcher来初始化Struts2并且处理所有的WEB请求 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
6、在Test\WEB-INF\classes目录下建立struts.xml文件。
7、实现业务控制器类:
- public class LoginAction {
- private String username;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String execute() throws Exception{
- if(getUsername().equals("username")) {
- return "success";
- } else {
- return "error";
- }
- }
- }
public class LoginAction {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String execute() throws Exception{
if(getUsername().equals("username")) {
return "success";
} else {
return "error";
}
}
}
编译,并将得到的类文件放入Test\WEB-INF\classes目录下。
8、在struts.xml文件中配置业务控制器
- <package name="Test" extends="struts-default">
- <action name="login" class="LoginAction">
- <result name="success">success.jsp</result>
- <result name="error">error.jsp</result>
- </action>
- </package>
<package name="Test" extends="struts-default"> <action name="login" class="LoginAction"> <result name="success">success.jsp</result> <result name="error">error.jsp</result> </action> </package>
9、编写jsp页面
index.jsp:
- <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- </head>
- <body>
- <s:form action="login.action">
- <s:textfield name="username" label="名字"/>
- <s:submit value="提交"/>
- </s:form>
- </body>
- </html>
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<s:form action="login.action">
<s:textfield name="username" label="名字"/>
<s:submit value="提交"/>
</s:form>
</body>
</html>
success.jsp:
- <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- </head>
- <body>
- <s:property value="username"/>,登陆成功
- </body>
- </html>
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<s:property value="username"/>,登陆成功
</body>
</html>
error.jsp:
- <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- </head>
- <body>
- <s:property value="username"/>,登陆失败
- </body>
- </html>
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<s:property value="username"/>,登陆失败
</body>
</html>
10、测试、完成。
==============================================================
http://chenguanwei2008.iteye.com/blog/356830