MyEclipse 下使用 Struts 2 开发
一 环境
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--->客户浏览器端显示
相关推荐
dingqinghu 2020-06-16
markzl 2020-04-30
lunareclipse 2020-04-23
zhangxiaocc 2020-04-23
荷塘月色 2020-03-07
xnuzfm 2020-03-07
markzl 2020-03-07
xnuzfm 2020-02-21
熊猫大哥大 2011-06-04
lizhenmxcz 2013-05-12
lunareclipse 2020-01-08
tdeclipse 2011-02-28
程序员超哥 2014-06-26
BruceWayne 2014-06-07
lunareclipse 2019-12-23
A.在myeclise中恢复例子开发环境:1>在myeclipse中新建一个web工程,项目名称自己任意取名,"J2EE Specification Level"一栏选择"J2EE1.4",然后点击"
简单就是美 2013-07-07
eclipsec 2014-01-26
heshizui 2013-09-04