关于Struts2框架的学习(一)

来源:互联网 发布:搭讪技巧 知乎 编辑:程序博客网 时间:2024/06/06 04:00

一、学习struts2

1.所用的jar包:

 

2.配置struts2的核心控制器web.xml文件

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"         version="3.1">    <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>
3.在src下添加struts2的配置文件struts.xml文件,此文件名不能改

<?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"><struts></struts>
此时,一个struts2的框架就已经搭建起来了。

4.编写Action类 ---- HelloAction 

package com.yaoyao;public class HelloAction {    public String execute(){        System.err.print("Hello Struts2");        return "success";    }}
在servlet中,默认执行service方法,在struts2中,默认执行execute方法。

在servlet中,service方法参数是HttpServletRequest和HttpServletResponse,无返回值。在strtus2中,execute方法都是public的,并且返回值都是String类型,而且方法都是没有参数的。

5.配置Action类,在struts.xml文件中配置。

  strtus2是基于包来管理的。

<?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"><struts>    <!--extends必须写,直接或间接继承struts-default name自定义-->    <package name="hello" extends="struts-default">        <!-- name请求名称,不要写 /; class对应action完全限定名-->        <action name="hello" class="com.yaoyao.HelloAction">            <!-- 结果集 name和对应action的方法的返回值匹配,默认是success -->            <result name="success">/index.jsp</result>        </action>    </package></struts>

一个简单的登录功能

1.web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"         version="3.1">    <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>*.action</url-pattern>    </filter-mapping></web-app>
2.LonginActon.java类

package com.yaoyao;public class LoginAction {    //表单域的名称和属性值一样即可    private String name;    private String pwd;    //处理方法    public String execute(){        if("zhangfei".equals(name) && "1111".equals(pwd)){            return "success";        }else {            return "failed";        }    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPwd() {        return pwd;    }    public void setPwd(String pwd) {        this.pwd = pwd;    }}
3.strtus2配置文件

<?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"><struts>    <!--extends必须写,直接或间接继承struts-default name自定义-->    <package name="hello" extends="struts-default">        <!-- name请求名称,不要写 /; class对应action完全限定名-->        <action name="hello" class="com.yaoyao.HelloAction">            <!-- 结果集 name和对应action的方法的返回值匹配,默认是success -->            <result name="success">/index.jsp</result>        </action>    </package>    <package name="default" namespace="/" extends="struts-default">        <action name="login" class="com.yaoyao.LoginAction">            <result name="success">/success.jsp</result>            <result name="failed">/login.jsp</result>        </action>    </package></struts>
4.login.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html>  <head>    <title>登录</title>  </head>  <body>   <form action="login.action" method="post">        用户名:<input type="text" name="name"><br>        密码:<input type="password" name="pwd"><br>        <input type="submit" value="登录">   </form>  </body></html>
注意:action的提交地址.action是扩展名,默认为.action;action的扩展名和web.xml中配置的strtus2的核心过滤器相匹配










0 0
原创粉丝点击