Struts2实例1--------登录案例的实现

本文介绍如何在Struts框架中进行多个XML文件的配置,并通过示例展示了依赖注入的概念及其实现方式。同时,提供了从登录页面到处理Action的具体代码实现。


注:如果在程序中需要写多个xml文件,则必须包含在struts.xml文件中,格式如下:

<include file="./struts01.xml"></include>

 

Index.jsp

 <body>

   Hello!成功啦

 </body>

 

Login.jsp

<body>

   <form action="login.action" method="get">

             用户名:<inputtype="text" name="userName" /><br/><br/>

             密码:<inputtype="text" name="userPass" /><br/><br/>

             <input type="submit"value="submit"/>"

   </form>

 </body>

 

LoginAction.class

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extendsActionSupport{

         //封装jsp页面提交的数据

         privateString userName;

         privateString userPass;

        

         publicvoid setUserName(String userName) {

                   this.userName= userName;

         }

         publicvoid setUserPass(String userPass) {

                   this.userPass= userPass;

         }

         publicString checkLogin(){

                   System.out.println(this.userName+"--------------------"+this.userPass);

                   returnSUCCESS;

         }

}

 

Struts.xml

<?xml version="1.0"encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

         "-//ApacheSoftware Foundation//DTD Struts Configuration 2.3//EN"

         "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<includefile="./struts03.xml"></include>

</struts>

 

Struts03.xml

<?xml version="1.0"encoding="GBK"?>

<!DOCTYPE struts PUBLIC

         "-//ApacheSoftware Foundation//DTD Struts Configuration 2.3//EN"

         "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

         <packagename="test" namespace="/"extends="struts-default">

                   <actionname="ioc" class="cn.csdn.hr.action.DemoAction"method="add">

                            <paramname="name">redarmy</param>

                            <resultname="msg">./index.jsp</result>

                   </action>

                   <actionname="login" class="cn.csdn.hr.action.LoginAction"method="checkLogin">

                            <result>./index.jsp</result>

                   </action>

         </package>

</struts>
注:

依赖注入(也称控制反转【IOC】):就是本身是不负责任依赖对象的创建和维护,依赖对象的创建及维护是由外部容器负责的。控制方向就转移到了外部容器,控制权的转移就是所谓的反转。

在运行期间,由外部容器动态将依赖对象注入到组件中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值