`
Danker.Dai
  • 浏览: 67352 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

MyEclipse 下使用 Struts 2 开发

阅读更多

一 环境
    MyEclipse 1.0 + Struts 2.2.1

二 添加Struts2.0框架

  1. MyEclipse 新建Web 工程
  2. 选中已创建好的Web工程,右键->右键菜单【MyEclipse】-【Add Struts Capabilities】打开添加Struts窗口,Struts specification选择struts 2.1


三 Struts2 开发 (登陆小程序)

  1.  创建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>
     
  2.  创建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>
     
  3. 创建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";
    	}
    
    }
    
     
  4. 配置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>
     
  5. 配置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>
    
     
  6. 运行项目,登陆成功,转到welcome.jsp页面,若登陆失败则返回到login.jsp页面

Struts2框架的大概处理流程如下: 

 

 1、加载类(FilterDispatcher) 

 

 2、读取配置(struts配置文件中的Action

 

 3、派发请求(客户端发送请求)  

 

  4、调用ActionFilterDispatcherstruts配置文件中读取与之相对应的Action )  

 

  5、启用拦截器(WebWork拦截器链自动对请求应用通用功能,如验证)  

 

  6、处理业务(回调Actionexecute()方法) 

 

 7、返回响应(通过execute方法将信息返回到FilterDispatcher) 

 

 8、查找响应(FilterDispatcher根据配置查找响应的是什么信息如:SUCCESSERROR,将跳转到哪个jsp页面) 

 9、响应用户(jsp--->客户浏览器端显示

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics