Struts2?学过或工作接触过java web的人都知道Struts的MVC,在全方位认识struts2之前,应该学会怎么用。
1、struts2Demo的代码结构:src【实现代码】、resources【配置文件】、lib【struts2的开发依赖jar包】。
2、struts2的启动配置:web.xml【启动监听器配置文件】、struts.xml【struts2的主要配置文件】和mgr.xml【后台模块配置】
web.xml【启动监听器配置文件】》
<pre name="code" class="html"><?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">
<!-- Struts2的主配置文件 -->
<struts>
<constant name="struts.serve.static.browserCache" value="false"/>
<constant name="struts.enable.DynamicMethodInvocation" value="true" /><!-- true:支持动态方法,false:不支持冻啊提方法 -->
<!-- 这里配置提醒:如果开启开发模式为false,配置文件的热加载可以为true;相反,反之。不然会出现启动时出错。 -->
<constant name="struts.devMode" value="true" /><!-- true:开发模式,false:非开发模式 -->
<constant name="struts.configuration.xml.reload" value="false"/><!-- 修改struts2配置文件时,是否热加载 -->
<constant name="struts.multipart.maxSize" value="2097152000" /><!-- 文件上传的最大值设置 -->
<constant name="struts.i18n.encoding" value="UTF-8" /><!-- 设置struts2处理请求时的编码处理 -->
<constant name="struts.action.extension" value="do,action" /><!-- 设置struts2处理请求后缀规则 -->
<!--
1、name值是用来区别模块。不如有些项目的前后台聚合在同一个项目,就需要用两个package,此时需要更具name来区分前后台的配置。
2、namespace的值可以用来更深入的模块化命名 。
3、继承struts-default【默认配置】。因为struts核心功能都是通过或继承struts-default配置里面的拦截器来实现的,如文件上传,参数封装,
数据检验等,所有通常都需要继承struts-default
-->
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<global-results><!-- 全局结果定义 -->
<result name="error">/WEB-INF/template/error/error.jsp</result><!-- 错误页面 -->
</global-results>
<global-exception-mappings><!-- 全局异常配置 -->
<exception-mapping exception="java.lang.Exception" result="error"/><!-- 错误页面统一配置 -->
</global-exception-mappings>
<!-- 设置首页 -->
<action name="index">
<result type="redirectAction"><!-- 设置结果类型 -->
<param name="actionName">IndexAction</param><!-- 处理的action -->
<param name="namespace">/mgr</param><!-- 处理action的命名空间 -->
</result>
</action>
</package>
<!-- 使用include加入模块xml -->
<include file="mgr.xml"/>
<!-- 可以继续添加package -->
</struts>
mgr.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>
<!-- mgr模块,直接继承主配置【default】 -->
<package name="mgr" namespace="/mgr" extends="default">
<action name="IndexAction" class="com.wrs.action.index.IndexAction">
<result>/WEB-INF/template/mgr/index.jsp</result>
</action>
<!-- 使用通配来处理登录请求 -->
<action name="login_*" method="{1}" class="com.wrs.action.login.LoginAction">
<result name="loginIn">/WEB-INF/template/mgr/loginIn.jsp</result>
<result name="loginOut">/WEB-INF/template/mgr/loginOut.jsp</result>
<result type="redirectAction">others</result>
</action>
<!-- 使用通配来处理其他请求 -->
<action name="*" class="com.wrs.action.others.OtherAction">
<result>/WEB-INF/template/others/{1}.jsp</result>
</action>
<!-- 可以继续添加Actions -->
</package>
</struts>
3、相关实现代码
基础控制类》
package com.wrs.action;
import com.opensymphony.xwork2.ActionSupport;
/**
* @function 基础控制类
* @author WRS
* @remark 基础控制类
* @version 1.0
* @since jdk1.6
* @datetime 2015年11月13日 下午2:27:04
* @copyright wrs.com (c) 2013
*/
public class BaseAction extends ActionSupport {
private static final long serialVersionUID = 1L;
}
首页处理》
package com.wrs.action.index;
import com.wrs.action.BaseAction;
/**
* @function 首页
* @author WRS
* @remark 首页
* @version 1.0
* @since jdk1.6
* @datetime 2015年11月13日 下午2:28:00
* @copyright wrs.com (c) 2013
*/
public class IndexAction extends BaseAction {
private static final long serialVersionUID = 1L;
/*
* 首页
* (non-Javadoc)
* @see com.opensymphony.xwork2.ActionSupport#execute()
*/
@Override
public String execute() throws Exception {
return super.execute();
}
}
登录处理》
package com.wrs.action.login;
import com.wrs.action.BaseAction;
/**
* @function 登录处理
* @author WRS
* @remark 登录处理:登入和登出
* @version 1.0
* @since jdk1.6
* @datetime 2015年11月13日 下午2:29:54
* @copyright wrs.com (c) 2013
*/
public class LoginAction extends BaseAction {
private static final long serialVersionUID = 1L;
public LoginAction() {
System.out.println("LoginAction()");
}
/**
* 登入
* @return
*/
public String userIn(){
return "loginIn";
}
/**
* 登出
* @return
*/
public String userOut(){
return "loginOut";
}
}
其他处理》
package com.wrs.action.others;
import com.wrs.action.BaseAction;
/**
* @function 其他处理
* @author WRS
* @remark 其他处理
* @version 1.0
* @since jdk1.6
* @datetime 2015年11月13日 下午2:32:20
* @copyright wrs.com (c) 2013
*/
public class OtherAction extends BaseAction {
private static final long serialVersionUID = 1L;
public OtherAction() {
System.out.println("OtherAction()");
}
/*
* 默认处理
* (non-Javadoc)
* @see com.opensymphony.xwork2.ActionSupport#execute()
*/
@Override
public String execute() throws Exception {
return super.execute();
}
}
友情提示:本人提供相关IT技术开发和支持,与其相关技术交流。
如需请加微信号: