Struts2简单Demo

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技术开发和支持,与其相关技术交流。

如需请加微信号:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单Struts2 示例,将输入的名称传递给 Action 类并在 JSP 页面上显示欢迎消息。 首先,我们需要创建一个名为 "WelcomeAction" 的 Action 类,在其中定义一个名为 "name" 的属性和一个名为 "execute" 的方法。 ```java package com.example.struts2demo; public class WelcomeAction { private String name; public String execute() { return "success"; } public String getName() { return name; } public void setName(String name) { this.name = name; } } ``` 接下来,我们需要创建一个名为 "welcome.jsp" 的 JSP 页面,该页面将显示欢迎消息,并使用 Struts2 标记库获取 Action 类中设置的名称。 ```jsp <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html> <html> <head> <title>Welcome</title> </head> <body> <h1>Welcome <s:property value="name" /></h1> </body> </html> ``` 接下来,我们需要创建一个名为 "struts.xml" 的配置文件,该文件将指定如何将请求映射到 Action 类以及如何处理结果。 ```xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="default" extends="struts-default"> <action name="welcome" class="com.example.struts2demo.WelcomeAction"> <result name="success">/welcome.jsp</result> </action> </package> </struts> ``` 最后,我们需要创建一个名为 "index.jsp" 的 JSP 页面,该页面将显示一个表单,允许用户输入他们的名称,并将其作为参数传递给 Action 类。 ```jsp <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html> <html> <head> <title>Welcome</title> </head> <body> <h1>Welcome</h1> <s:form action="welcome"> <s:textfield name="name" label="Enter your name" /> <s:submit value="Submit" /> </s:form> </body> </html> ``` 现在,我们已经完成了 Struts2 示例的所有必要组件。要运行示例,请将这些文件保存在您的 Web 应用程序中,并使用您喜欢的 Web 服务器启动应用程序。然后,导航到 "index.jsp" 页面,输入您的名称并单击提交按钮,应该会显示一个欢迎消息,其中包含您输入的名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值