struts2是由webwork+struts1得来的。很多特性与webwork相似,但与struts1有很大的区别.
我以前没搞过什么webwork(
http://baike.baidu.com/view/25660.htm)所以对struts2的机制还很莫生。
运行环境:
servlet contant : apache-tomcat-6.0.20
IDE: myeclipse7.0 (配套的eclipse为3.4)
JDK: jdk1.6.0_03
---------------------------
1. 创建web项目
struts2Test
1.1 在WEB-INF下的lib中拷入struts2的jar包。会在Referenced Libraries中显示。
lib中不会显示,当程序发布后在tomcat中会显示这会jar包.
1.2 项目的目录结构。如下图:
2. 在WebRoot下面建3个jsp文件.
2.1 login.jsp
Code
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ 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 'login.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">
</head>
<body>
<s:form name="pageform" action="login" namespace="/" method="post">
<s:textfield name="username" label="用户名" />
<s:password name="password" label="密码aa" />
<s:submit value="登录" />
</s:form>
</body>
</html> 2.2 success.jsp 内容随便去写吧。
2.3 failed.jsp
3. 修改WEB-INF下面的web.xml文件
Code
<?xml version="1.0" encoding="UTF-8"?>
<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-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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>、
这个可以不写
4. 在src下建立action类
在src下建立包com.web 然后建立struts2Action.java类
Code
package com.web;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class struts2Action extends ActionSupport {
private String username;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
// @Override
public String execute() throws Exception {
if (this.getUsername().equals("wj")
&& this.getPassword().equals("1234")) {
ActionContext.getContext().getSession().put("user",
this.getUsername());
return "success";
} else {
return "error";
}
}
// @Override
public void validate() {
if (this.getUsername() == null || this.getUsername().trim().equals("")) {
addFieldError("username", "用户名错误!");
}
if (this.getPassword() == null || this.getPassword().trim().equals("")) {
addFieldError("password", "password 错误!");
}
}
}
5. 在src下建立 struts.xml文件
Code
<?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="login" namespace="/" extends="struts-default">
<action name="login" class="com.web.struts2Action">
<result name="error">failed.jsp</result>
<result name="success">success.jsp</result>
<result name="input">login.jsp</result>
</action>
</package>
</struts> 6. 发布运行
6.1 在如: D:\tomcat\apache-tomcat-6.0.20\conf 的下面有个server.xml 修改成
docBase为项目所在的位置 reloadable为true时不用返复重启tomcat 一般编写代码时写成true 发布后写成false以保存程序运行的稳定性.
<Context path="/struts2Test" docBase="D:\project\java_workspace\struts2Test\WebRoot" reloadable="true"/>
-->
</Host>
</Engine>
</Service>
</Server>
6.2
http://localhost:8088/struts2Test/login.jsp 点按钮提交后会变成
http://localhost:8088/struts2Test/login.action