1、打开myeclipse开发环境(我的myeclipse是8.5版本),新建一个web项目(本次所有的过程都会有图),然后window->preferences->Server->Tomcat->Tomcat6.0,然后选中Enable,并且找到你所装的tomcat的目录。点击ok
2、点击window->preference->java->Installed Jres->然后add你的jdk位置,(我的是D:\java)->finish
3、拷贝struts的配置文件struts.xml到新建的项目src中,该文件在:http://pan.baidu.com/s/1qWPPaig 百度网盘
4、struts环境安装所需的jar文件(类库):http://pan.baidu.com/s/1zTeYq 百度网盘
5、将WebRoot->WEB-INF下面的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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
这里需要将index.jsp 更名为Hello.jsp(,因为刚刚上面的struts.xml 中是Hello.jsp)
然后在浏览器中输入:http://localhost:8888/Struts2_0100_Introduction/hello.action(因为在struts.xml中有一个namespace=”/“)
输出解果是:
================中间过程的几个步骤截图如下:===============
配置环境下面就来看一下实例吧!
1. 配置web.xml文件。所有的web项目都是以web.xml文件为起点的,所有的起始配置都在这个文件中进行,而所谓的起始配置则是指在服务器启动时所读取的配置。比如我们经常所用到的框架的配置,servlet的配置,filter的配置等信息。Struts2的web.xml文件与struts1的有所不同,使用struts1框架时用servlet标签进行配置来关联项目和框架,而使用struts2用的是filter标签来关联项目和框架。总的来说原理相同,都是通过映射来制定哪些类型的页面需要交给框架去处理(在下文实例中会具体给出配置信息)。
2. 需要被框架核心类读取的xml文件的配置。在struts1中这个xmL文件名称为struts-config,而struts2中该文件的名称为struts.xml。名字的差异无关紧要,重要的是该文件中的配置信息有所不同,主要在于Struts-config中我们定义了很多的ActionForm以及Action类,这些Action必须继承ActionServlet抽象类,但是在struts2中已经没有了ActionForm这个概念,原先ActionForm所承担的职责已经被转移到Action中,并且struts2中的Action支持完全的POJO对象,当然也可以继承相应的ActionSupport类。
3. 一个登录实例
Web.xml文件配置信息如下
<span style="font-family:Microsoft YaHei;font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SSH5</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app></span>
Struts.xml配置信息如下:
<span style="font-family:Microsoft YaHei;font-size:14px;"><?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.action.extension" value=","></constant>
<package name="testLogin" namespace="/" extends="struts-default">
<action name="login" class="com.action.LoginAction">
<result name="success">/user.jsp</result>
<result name="error" type="redirect">/error.jsp</result>
<result name="checkError">/checkSession.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts></span>
LoginAction类:
<span style="font-family:Microsoft YaHei;font-size:14px;">package com.action;
import com.entity.User;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String execute() throws Exception {
if("admin".equals(user.getUsername()) && "admin".equals(user.getPassword())){
System.out.println("登录成功");
return "success";
}else{
System.out.println("登录失败");
return "error";
}
}
}</span>
然后是jsp页面:
<span style="font-family:Microsoft YaHei;font-size:14px;"><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login">
登录页面<br>
用户名:<input name="user.username" type="text" ><br>
密码:<input name="user.password" tyep="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html></span>
上述登录实例的执行过程为当启动TomCat的时候TomCat首先加载web.xml,根据web.xml中的配置信息由
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter类读取struts.xml中的信息,然后根据web.xml文件中规定的拦截文件路径确定哪些页面将会交由struts框架进行处理,一般配置成/*,表示将对所有的页面进行出拦截处理,然后根据jsp页面中的提交路径以及struts.xml文件中的配置信息确定该页面将提交给哪个Action。当所提交的请求被Action处理后,将根据执行结果返回相应的字符串,根据返回值将决定具体的跳转页面。
注:本实例前半部分自己总结,后半部分从其他博客学习而来。希望对大家有帮助。