Struts2基础篇(1)

一、Struts2简介

Struts2以WebWork优秀的设计思想为核心,吸收了Struts1的部分优点,建立了一个基于WebWork和Struts1的MVC框架。

二、搭建Struts2开发环境

2.1、通过官网下载最新版:http://struts.apache.org/download.cgi

建议下载struts-xx.all.zip包,压缩包中不仅包含struts2的jar还包含了示例、源码以及帮助文档。

2.2、在项目中导入Struts2需要的jar包

2.3、修改web.xml文件

在web.xml文件的<web-app>节点下添加StrutsPrepareAndExecuteFilter核心过滤器,主要负责拦截用户的请求,交给Struts2的框架进行处理。

2.4、添加struts.xml配置文件。

struts.xml是Struts2的核心配置文件,该文件通常放在src目录下,编译部署以后,他就到了应用程序的WEB-INF\classes目录下。

三、使用struts2输出Hello World

3.1、新建web项目,并导入struts的jar包

  

3.2、添加Action类

实现Action可以有三种方法:

1.使用普通的Java类,编写public String execute()方法

2.实现Action接口,实现execute()方法

3.继承ActionSupport类,重写execute()方法。

package com.able.action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 
 * @author 夏中伟
 * 实现Action可以有三种方法:
 * 1.使用普通的Java类,编写public String execute()方法
 * 2.实现Action接口,实现execute()方法
 * 3.继承ActionSupport类,重写execute()方法。
 * userAction这里继承ActionSupport
 */
public class UserAction extends ActionSupport {
	private String name;
	private String message;

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * 这个方法名可以随便写
	 * @return
	 */
	public String execute(){
		System.out.println(name+"-----------------");
		message="Hello World"+name;
		return "abc";
	}
}

 3.3、修改web.xml

<?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>Struts2_Demo</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>

3.4、在src目录下添加struts.xml,并添加相应的配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <!-- 修改了Struts.xml需要重启web服务配置才能生效,为了避免多次重启web服务设置以下常量 -->
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">

        <default-action-ref name="index" />

        <global-results>
            <result name="error">/err.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>
        
        <!-- 执行的是代理类 -->
       <action name="userAction" class="com.able.action.UserAction" method="execute">
       	<result name="abc" >index.jsp</result> <!--type="forword"-->
       </action>
       <action name="login" class="com.able.action.LoginFieldAction">
       	<result name="success">index.jsp</result>
       </action>
        
    </package>
	<!-- <include file="exemple.xml"></include> -->
    <!-- Add packages here -->

</struts>

3.5、修改index.jsp文件

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
  	<form action="userAction">
  		用户名:<input name="name"/><br/>
  		<input type="submit" value="提交"/>
  	</form>
  	<div>
  		<s:property value="message"/>
  	</div>
  	<div id="通过属性传值,以及modelDriven的接收">
  		刚刚输入的用户名:
  		<s:property value="username"/><br/>
  		<s:property value="password"/>
  	</div>
  	**********************分割线*************************
  	<div>
  		这是通过javaBean方式获取的用户名和密码
  		<s:property value="user.username"/><br/>
  		<s:property value="user.password"/>
  	</div>
  </body>
</html>

3.6、将项目添加到tomcat中启动项目,在浏览器中输入:localhost:8080/Struts2_Demo(自己的项目名)/userAction

就可以看到页面跳转到index.jsp页面,然后在文本框中输入xiazhongwei提交后,在下边输出message,

四、总结

4.1、在浏览器中请求userAction时会经过Struts的核心过滤器“StrutsPrepareAndExecuteFilter”,

4.2、核心过滤器会根据请求在struts.xml中匹配name相同的action,然后会跳转到相应action处理类userAction类,

4.3、会在userAction寻找与struts.xml中配置的<action method=“exectue”>标签中属性method相同值的方法,默认执行行方法名是execute()方法,如果在标签中定义method="add",就回在userAction方法中寻求add的方法。

4.4、执行完execute()方法后根据返回值String,寻找匹配struts.xml中<action></action>标签中的<resutl>值相同的result

4.5、根据类型跳转相应的请求中,上示例定义跳转到index.jsp中,所以会在页面中看到userAction类中execute方法中返回的message值“Hello world  ”+name,name是页面提交表单中的值

posted @ 2016-07-03 12:47  夏中伟  阅读(467)  评论(0编辑  收藏  举报