struts-default.xml
来源:互联网 发布:游戏美工培训班 编辑:程序博客网 时间:2024/04/28 00:38
1. 引入jar包(这里以struts2.2.3为例),所需jar包有以下几个:
包名
说明
commons-logging-1.0.4.jar
日志管理
freemarker-2.3.8.jar
表现层框架,定义了struts2的可视组件主题(theme)
ognl-2.6.11.jar
OGNL表达式语言,struts2支持该EL
struts2-core-2.0.10.jar
struts2的核心库
xwork-2.0.4.jar
webwork的核心库,自然需要它的支持
2. 配置web.xml文件。所有的web项目都是以web.xml文件为起点的,所有的起始配置都在这个文件中进行,而所谓的起始配置则是指在服务器启动时所读取的配置。比如我们经常所用到的框架的配置,servlet的配置,filter的配置等信息。Struts2的web.xml文件与struts1的有所不同,使用struts1框架时用servlet标签进行配置来关联项目和框架,而使用struts2用的是filter标签来关联项目和框架。总的来说原理相同,都是通过映射来制定哪些类型的页面需要交给框架去处理(在下文实例中会具体给出配置信息)。
3. 需要被框架核心类读取的xml文件的配置。在struts1中这个xmL文件名称为struts-config,而struts2中该文件的名称为struts.xml。名字的差异无关紧要,重要的是该文件中的配置信息有所不同,主要在于Struts-config中我们定义了很多的ActionForm以及Action类,这些Action必须继承ActionServlet抽象类,但是在struts2中已经没有了ActionForm这个概念,原先ActionForm所承担的职责已经被转移到Action中,并且struts2中的Action支持完全的POJO对象,当然也可以继承相应的ActionSupport类。
完成了上面的配置后接下来就是编写我们的Action类了,下面通过一个登陆实例来介绍struts2的使用。
Web.xml文件配置信息如下:
- <span style="font-family:Microsoft YaHei;font-size:14px;"><?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>SSH5</display-name>
- <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></span>
Struts.xml配置信息如下:
- <span style="font-family:Microsoft YaHei;font-size:14px;"><?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>
- <constant name="struts.action.extension" value=","></constant>
- <package name="testLogin" namespace="/" extends="struts-default">
- <action name="login" class="com.action.LoginAction">
- <result name="success">/user.jsp</result>
- <result name="error" type="redirect">/error.jsp</result>
- <result name="checkError">/checkSession.jsp</result>
- <interceptor-ref name="defaultStack"></interceptor-ref>
- </action>
- </package>
- </struts></span>
LoginAction类:
- <span style="font-family:Microsoft YaHei;font-size:14px;">package com.action;
- import com.entity.User;
- import com.opensymphony.xwork2.ActionSupport;
- public class LoginAction extends ActionSupport {
- private User user;
- public User getUser() {
- return user;
- }
- public void setUser(User user) {
- this.user = user;
- }
- @Override
- public String execute() throws Exception {
- if("admin".equals(user.getUsername()) && "admin".equals(user.getPassword())){
- System.out.println("登录成功");
- return "success";
- }else{
- System.out.println("登录失败");
- return "error";
- }
- }
- }</span>
最后是jsp页面代码:
- <span style="font-family:Microsoft YaHei;font-size:14px;"><%@ 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>Insert title here</title>
- </head>
- <body>
- <form action="login">
- 登录页面<br>
- 用户名:<input name="user.username" type="text" ><br>
- 密码:<input name="user.password" tyep="password"><br>
- <input type="submit" value="登录">
- </form>
- </body>
- </html></span>
上述登录实例的执行过程为当启动TomCat的时候TomCat首先加载web.xml,根据web.xml中的配置信息由org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter类读取struts.xml中的信息,然后根据web.xml文件中规定的拦截文件路径确定哪些页面将会交由struts框架进行处理,一般配置成/*,表示将对所有的页面进行出拦截处理,然后根据jsp页面中的提交路径以及struts.xml文件中的配置信息确定该页面将提交给哪个Action。当所提交的请求被Action处理后,将根据执行结果返回相应的字符串,根据返回值将决定具体的跳转页面。
- struts-default.xml
- struts-default.xml
- struts-default.xml
- struts-default.xml详解
- struts-default.xml
- struts-default.xml文档解释
- struts2 struts-default.xml 学习
- struts.xml和default.properties
- struts-default.xml文件详解
- struts2 default.properties struts.properties struts.xml
- struts-default.xml和struts.properties
- Struts2中有关struts-default.xml,struts.xml,struts.pro
- Struts2中有关struts-default.xml,struts.xml,struts.pro
- 默认配置文件struts-default.xml解析
- struts-default.xml中的result配置
- struts-default.xml 文件说明
- struts2默认配置文件 struts-default.xml 研究
- struts-default.xml位置未知的问题
- 定义一个线性表接口
- nyoj 24
- java编程思想11.10-map
- JSP 中文乱码处理解决方案
- Activity切换动画、overridePendingTransition、fragment动画
- struts-default.xml
- mybatis oracle 新增返回主键
- macbook下显示系统隐藏目录和文件
- Scroll原理-附ScrollView源码分析
- 处理URL传递中文乱码问题
- CC2530 DB EB
- Wireshark基本介绍和学习TCP三次握手
- Makefile 文件名操作函数
- Web.xml中Filter过滤器标签几个说明