Struts2 使用案例(操作流程)

来源:互联网 发布:python enroll 编辑:程序博客网 时间:2024/06/03 21:49

Struts2 使用案例(操作流程)

使用版本:struts-2.3.24-all

下面是演示测试项目结构:

使用案例(操作流程):


 步骤一:准备jar包(log4j.jar为日志包),并将所有的jar包加载到项目中(全选右键---> Build Path ---> Add to Build Path)。

     

  

 步骤二:在web.xml中配置过滤器

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"id="WebApp_ID" version="3.1"><display-name>Struts2Week</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><!--配置过滤器 --><filter><filter-name>front</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>front</filter-name><url-pattern>*.action</url-pattern></filter-mapping></web-app>


<filter-class>的内容可以在如下图中查找:

 


步骤三:接下来演示请求和响应操作

1、准备了一个请求的jsp页面index.jsp和响应的页面main.jsp

index.jsp  (请求页面)

<%@ 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></title></head><body><h1>这是index页面</h1><a href="user/login.action">测试strut</a><!--user表示命名空间,login.action表示请求的action  --></body></html>


main.jsp (响应页面)

<%@ 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></title></head><body><h1>这是main页面</h1></body></html>



2:准备一个Struts的主配置文件  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></struts>



  3:创建一个action类

package cn.sz.action;public class UserAction {public String login() {return "success";}}

4、重新配置主配置文件 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><package name="user" extends="struts-default" namespace="/user"><action name="login" class="cn.sz.action.UserAction" method="login"><result>/main.jsp</result></action></package></struts>

相关标签及属性简介:

package: 包,action必须放在包下面,包也就是用来做分类的。
常用属性:
name:必填属性,用来指定包的名字。
extends:可选属性,用来指定该包继承其他包,这里继承的是struts-default。
namespace: 可选属性,定义package命名空间 该命名空间影响到url的地址,例如此命名空间为/test那么访问是的地址为http://localhost:8080/struts2/test/XX.action
abstract:设置package的属性为抽象的 抽象的package不能定义action 值true:false

action: 定义处理请求URL为login.action的Action 。
常用属性:
name: 请求的名称
class: 请求处理的类
method: 请求处理的方法


result: 定义处理结果字符串和资源之间的映射关系
常用属性:
name:  result名称 和Action中返回的值相同,不写默认  name="success";
type:   处理结果类型,默认为dispatcher



完成代码后将项目部署到服务器,将默认打开index.jsp 请求页面,当你点击里面的请求连接之后就会请求action类,然后转发到main.jsp页面。

      




这就是一个简单的struts2使用流程。