一 环境
MyEclipse 1.0 + Struts 2.2.1
二 添加Struts2.0框架
- MyEclipse 新建Web 工程
- 选中已创建好的Web工程,右键->右键菜单【MyEclipse】-【Add Struts Capabilities】打开添加Struts窗口,Struts specification选择struts 2.1
三 Struts2 开发 (登陆小程序)
- 创建login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Login</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 action="/login" method="post"> <s:label value="系统登陆"></s:label> <s:textfield name="username" label="账号" /> <s:password name="password" label="密码" /> <s:submit value="登录" /> </s:form> </body> </html>
- 创建welcome.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'welcome.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>欢迎${username }! </body> </html>
- 创建Action类 LoginAction
package com.influx.struts2.action; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport {// 该类继承了ActionSupport类。这样就可以直接使用SUCCESS, // LOGIN等变量和重写execute等方法 private static final long serialVersionUID = 1L; 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; } @Override public String execute() throws Exception { if ("haha".equals(username) && "hehe".equals(password))// 如果登录的用户名=haha并且密码=hehe,就返回SUCCESS;否则,返回LOGIN return "success"; return "login"; } }
- 配置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> <package name="default" namespace="/" extends="struts-default"> <action name="login" class="com.influx.struts2.action.LoginAction" method="execute"> <result name="success">/welcome.jsp</result> <result name="login">/login.jsp</result> </action> </package> </struts>
- 配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"> <display-name></display-name> <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>
- 运行项目,登陆成功,转到welcome.jsp页面,若登陆失败则返回到login.jsp页面
Struts2框架的大概处理流程如下:
1、加载类(FilterDispatcher)
2、读取配置(struts配置文件中的Action)
3、派发请求(客户端发送请求)
4、调用Action(FilterDispatcher从struts配置文件中读取与之相对应的Action )
5、启用拦截器(WebWork拦截器链自动对请求应用通用功能,如验证)
6、处理业务(回调Action的execute()方法)
7、返回响应(通过execute方法将信息返回到FilterDispatcher)
8、查找响应(FilterDispatcher根据配置查找响应的是什么信息如:SUCCESS、ERROR,将跳转到哪个jsp页面)
9、响应用户(jsp--->客户浏览器端显示
相关推荐
MyEclipse8下struts2开发例程及解析,绝对值得收藏学习!
MyEclipse8 已经支持struts 2,但是现有的资料包括书籍、视频、网络资料均很少有人介绍在MyEclipse8 环境下的struts 2开发,甚至包括MyEclipse8 自带...本人综合各方面资料,对MyEclipse8 下的struts开发过程进行整理。
MyEclipse开发Struts2
王者归来之JAVA程序开发大全:基于MYECLIPSE平台+STRUTS+HIBERNATE+SPRING主体流框架开发.pdf MyEclipse使用最好的资源!
初学者struts2 myeclipse开发实例,登陆界面
利用Myeclipse快速开发struts应用程序
struts2国际化MyEclipse开发struts2国际化MyEclipse开发童叟无欺
myEclipse中配置struts开发环境
MyEclipse 2014 WEB开发配置Struts2+Spring+Hibernate 最基本应用,软件自动配置原型,并能运行。
利用Myeclipse快速开发struts应用程序.ppt对于初学STRUTS 的人来产有很大的帮助……
MyEclipse下配置Struts2[整理].pdf
MyEclipse开发SSH2(Struts2+Spring+Hibernate)教程
MyEclipse 10 下的Struts 2 开发范例(一)源代码,根据其文章写的,导入工程可直接跑
使用MyEclipse6开发Struts+Spring应用程序示例代码工程
java web 程序开发大全 基于myeclipse平台+struts+hibernate+spring主流框架开发的代码,是一本很好的入门的书,因为大小的关系,分两部分,这是part1,到16章
一个简单但完整的struts 2的开发范例与教程,很详细,这是系列文章的第一篇
MyEclipse开发Struts+Hibernate应用,图文示例
这是一个由Struts2和Hibernate整合而成的新闻发布系统,采用MyEclipse9.0开发工具和Mysql数据库,附有数据库文件,完整的源代码,很适合初学者学习使用
MyEclipse开发Struts2详细图解过程