先运行一个小的简单的Struts程序来看看它是怎么配置和运行的:
步骤如下:
步骤一:配置struts所需要的环境:
把5个开发包.(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)导入到Web项目中,导入方法有两种:一种是右键项目名--àProperties--àJava Build Path -àAdd External JARS…… 来导入所需的包,第二种是直接把5个包copy到Web项目的lib下即可.
步骤二: 开发一个前台JSP页面,文件名为login.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ 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>
<title>My JSP 'login.jsp' starting page</title>
</head>
<body>
<s:form action="login.action" method="post">
<s:textfield name="username" label="username" ></s:textfield>
<s:textfield name="password" label="password"></s:textfield>
<s:submit label="submit"></s:submit>
</s:form>
</body>
</html>
步骤三:开发一个Action,文件名为LoginAction.java
package com.xgj;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() throws Exception {
return SUCCESS;
}
}
}
步骤四:写配置文件struts.xml,该文件要置于class文件的根目录下,用ecliplse开发时,写在src根目录下就行了:
<?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>
<package name="struts2" extends="struts-default">
<action name="login" class="com.xgj.LoginAction">
<result name="success">result.jsp</result>
</action>
</package>
</struts>
步骤五:启动Tomcat访问login.jsp页面即可.