struts.xml详解
参考自:http://blog.csdn.net/zz_mm/article/details/5460397
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
<constant name="struts.devMode" value="true"></constant> <package name="default" namespace="/" extends="struts-default">
<!-- 注册拦截器 -->
<interceptors>
<interceptor name="authi" class="interceptor.authInterceptor"></interceptor>
<!-- 自定义拦截器栈 -->
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="authi"></interceptor-ref>
</interceptor-stack>
</interceptors> <action name="auth">
<result>/WEB-INF/pages/manager.jsp</result>
<result name="login">/login.jsp</result>
<!-- 引用自定义拦截器栈 -->
<interceptor-ref name="myStack"></interceptor-ref>
</action> <action name="login" class="action.loginAction" method="login">
<result name="success">/WEB-INF/pages/manager.jsp</result>
<result name="error">/login.jsp</result>
</action>
</package> </struts>
如上示例的配置,配置了一个名为default的包,该包下定义了一个Action和interceptor。
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- struts2的action必须放在一个指定的包空间下定义 -->
<package name="qiujy" extends="struts-default">
<!-- 定义处理请求URL为login.action的Action -->
<action name="login" class="org.qiujy.web.struts2.action.LoginAction">
<!-- 定义处理结果字符串和资源之间的映射关系 -->
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package> <package name="my" extends="struts-default" namespace="/manage">
<!-- 定义处理请求URL为login.action的Action -->
<action name="backLogin" class="org.qiujy.web.struts2.action.LoginAction">
<!-- 定义处理结果字符串和资源之间的映射关系 -->
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package></struts>
<struts>
<include file="struts-default.xml"/>
<include file="struts-user.xml"/>
<include file="struts-book.xml"/>
<include file="struts-shoppingCart.xml"/> ......
</struts>
1.4. 拦截器配置:
<struts>
......
<constant name="struts.custom.i18n.resources" value="messages"/>
</struts>
表示指定了资源文件的放置在classes目录下,基本名是messages,则在classes目录下您就应该放置类似messages_zh_CN.properties,message_en.properties名的文件。
package org.qiujy.web.struts2.action; import com.opensymphony.xwork2.ActionSupport; /**
*@authorqiujy
*@version1.0
*/
publicclass LoginAction extends ActionSupport{
private String userName;
private String password; private String msg; //结果信息属性 /**
*@returnthemsg
*/
public String getMsg() {
returnmsg;
}
/**
*@parammsgthemsgtoset
*/
publicvoid setMsg(String msg) {
this.msg = msg;
}
/**
*@returntheuserName
*/
public String getUserName() {
returnuserName;
}
/**
*@paramuserNametheuserNametoset
*/
publicvoid setUserName(String userName) {
this.userName = userName;
}
/**
*@returnthepassword
*/
public String getPassword() {
returnpassword;
}
/**
*@parampasswordthepasswordtoset
*/
publicvoid setPassword(String password) {
this.password = password;
} /**
*处理用户请求的excute()方法
*@return结果导航字符串
*@throwsException
*/
public String execute() throws Exception{
if("test".equals(this.userName) &&
"test".equals(this.password)){
msg = "登录成功,欢迎" + this.userName;
returnthis.SUCCESS;
}else{
msg = "登录失败,用户名或密码错";
returnthis.ERROR;
}
}
}
public String execute() throws Exception{
if("test".equals(this.userName) && "test".equals(this.password)){
msg = "登录成功,欢迎" + this.userName;
//获取ActionContext实例,通过它来访问Servlet API
ActionContext context = ActionContext.getContext();
//看session中是否已经存放了用户名,如果存放了:说明已经登录了;
//否则说明是第一次登录成功
if(null != context.getSession().get("uName")){
msg = this.userName + ":你已经登录过了!!!";
}else{
context.getSession().put("uName", this.userName);
} return this.SUCCESS;
}else{
msg = "登录失败,用户名或密码错";
return this.ERROR;
}
}
Struts2中通过ActionContext来访问Servlet API,让Action彻底从Servlet API 中分离出来,最大的好处就是可以脱离Web容器测试Action。
<form method="post" action="userOpt!login.action"> |
struts.xml详解的更多相关文章
- struts2之配置文件struts.xml详解
struts配置文件 struts.xml配置参数详解 struts.xml中很大一部分配置默认配置就好了 但是有些还是需要做了解 以便于理解 和修改 <?xml version=" ...
- Struts2初学 struts.xml详解 一
一.简介 Struts 2是一个MVC框架,以WebWork设计思想为核心,吸收了Struts 1的部分优点 二.详解 首先让我们看一下一个简单的struts.xml文件的结构 < ...
- Struts2初学 Struts.xml详解二
A.使用继承实现设置全局视图 package节点中还可以设置全局的视图,如: <global-results> <result name="e ...
- strus2 struts.xml详解
<struts> <!-- 配置一个包:package --> <package name="demo1" extends="struts- ...
- Struts功能详解——ActionMapping对象
Struts功能详解——ActionMapping对象 ActionMapping描述了struts中用户请求路径和Action的映射关系,在struts中每个ActionMapping都是通过pat ...
- Web.xml详解(转)
这篇文章主要是综合网上关于web.xml的一些介绍,希望对大家有所帮助,也欢迎大家一起讨论. ---题记 一. Web.xml详解: (一) web.xml加载过程(步骤) 首 ...
- Maven-pom.xml详解
(看的比较累,可以直接看最后面有针对整个pom.xml的注解) pom的作用 pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者需要遵 ...
- 【maven】 pom.xml详解
pom.xml详解 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...
- build.xml详解
build.xml详解1.<project>标签每个构建文件对应一个项目.<project>标签时构建文件的根标签.它可以有多个内在属性,就如代码中所示,其各个属性的含义分别如 ...
随机推荐
- SiteMesh2-sitemesh.xml的ParameterDecoratorMapper映射器的用法
继续使用上一章http://www.cnblogs.com/EasonJim/p/7086916.html的例子,改造成使用ParameterDecoratorMapper映射器的方法,这个映射器不需 ...
- Microsoft SQL Server Query Processor Internals and Architecture
https://msdn.microsoft.com/en-us/library/aa226174(v=sql.70).aspx
- PHP array_keys()
定义和用法 array_keys() 函数返回包含数组中所有键名的一个新数组. 如果提供了第二个参数,则只返回键值为该值的键名. 如果 strict 参数指定为 true,则 PHP 会使用全等比较 ...
- Java深入浅出系列(四)——深入剖析动态代理--从静态代理到动态代理的演化
静态代理 如上图,在程序执行之前.程序猿就要编写Proxy.然后进行编译,即在程序执行之前,代理类的字节码文件就已经生成了(Proxy类的class文件已经存在了). 静态代理尽管在增强现有的接口业务 ...
- CentOS7安装MariaDB成功的实践
前言 在自己的VPS的CentOS7安装Oracle的Mysql失败以后,我又开始找CentOS7上面安装MariaDB的方法,于是我找打了这篇文章:http://blog.csdn.net/defa ...
- LeetCode 739. Daily Temperatures (每日温度)
题目标签:HashMap 题目给了我们一组温度,让我们找出 对于每一天,要等多少天,气温会变暖.返回一组等待的天数. 可以从最后一天的温度遍历起,从末端遍历到开头,对于每一天的温度,把它在T里面的in ...
- B. Case of Fake Numbers( Codeforces Round #310 (Div. 2) 简单题)
B. Case of Fake Numbers time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- C语言控制台窗体图形界面编程(总结)
本系列文章是笔者通过学习<C语言控制台窗体界面编程(修正版)>而写(关于此文档的很多其它信息请看本系列文章第一篇),旨在让大家更加清晰简洁easy地学习C语言控制台窗体界面的编程. 通过本 ...
- 42.extjs Combobox动态加载数据问题,mode:local 还是remote
问题: Java代码 var fabircTypeDs = new Ext.data.Store({ proxy: new Ext.data.HttpProxy({ url: 'province. ...
- Magnetic Storms
http://acm.timus.ru/problem.aspx?space=1&num=1126 简单的线段树求区间最值 #include <stdio.h> #include ...