zoukankan      html  css  js  c++  java
  • Struts2-初体验

    Struts 2的起源和背景

    Struts 2以WebWork优秀的设计思想为核心,吸收了Struts 1的部分优点,建立了一个兼容WebWork和Struts 1的MVC框架,Struts 2的目标是希望可以让原来使用Struts 1、WebWork的开发人员,都可以平稳过渡到使用Struts 2框架。

    建立Struts 2的应用程序步骤

    1.创建一个Web工程;
    2.将需要用到的支持Struts2功能的jar包复制到此web工程的WEB-INF/lib目录底下;
    3.在WEB-INF/web.xml文件中为Struts的核心控制器配置FilterDispatcher过滤器映射
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     5     id="WebApp_ID" version="2.5">
     6 
     7     <display-name>Struts2</display-name>
     8 
     9     <!-- Struts2配置 -->
    10     <filter>
    11         <filter-name>struts2</filter-name>
    12         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    13     </filter>
    14     <filter-mapping>
    15         <filter-name>struts2</filter-name>
    16         <url-pattern>/*</url-pattern>
    17     </filter-mapping>
    18     <!-- -->
    19     <!-- org.apache.struts2.dispatcher.FilterDispatcher -->
    20 
    21     <welcome-file-list>
    22         <welcome-file>index.jsp</welcome-file>
    23     </welcome-file-list>
    24 
    25 </web-app>
    web.xml
    4.在ClassPath中创建Struts的配置文件struts.xml
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
     3     "http://struts.apache.org/dtds/struts-2.0.dtd">
     4 <struts>
     5     <package name="packageName" extends="struts-default">
     6         <action name="LogicName" class="CustomerAction">
     7             <result name="ResultString">Jsp file</result>
     8             <result name="Result">Jsp file</result>
     9         </action>
    10     </package>
    11 </struts>
    struts.xml
    —package:用于组织Action的一个逻辑概念,必须通过extends继承自struts-default包, struts-default包在struts-default.xml文件中定义,该文件配置了很多有用的功能。
    —action: 所有的Action都必须通过action标签配置
    name: 访问action 逻辑名称
    class: Action类的全限定名
    result子标签: 定义跳转路径, execute()方法的返回值在此定义。默认情况下为转发,如果是重定向,必须按下面的格式:
    <result type="redirect" name="xx">/xx.jsp</result>
    5.写资源文件(.properties)
    内容包括JSP页面的label内容、错误描述等..  
    保存在classpath下或package中
    在struts.xml中配置成constant element   
    6.视图层:JSP文件
    可在JSP中使用Struts自定义标签库中的标签
     1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
     2 <%@taglib prefix="s" uri="/struts-tags" %>
     3 <html>
     4     <head><title>Insert title here</title></head>
     5     <body>
     6         <h3><s:text name=“login.title”/></h3>
     7          <s:form action="login">
     8              <s:textfield name="name" key="login.name“/>
     9             <s:password name="password" key=“login.password“/>
    10              <s:submit value="Submit"/>
    11          </s:form>
    12     </body>
    13 </html>
    jsp文件
    Actions
    尽量在Action的execute()方法中调用其他对象(POJOs,EJBs等等)来处理业务逻辑。
    execute()方法会被自动调用,该方法用于处理用户请求,返回类型为String,是跳转路径的逻辑名称。
    含有execute()方法的POJO类就可以作为Action,无需实现任何接口或继承任何类,但有时为了简化某些功能必须继承ActionSupport类。
    每一个Action都要在struts.xml中进行配置
     1 public class LoginAction{
     2     private String name;
     3     private String password;
     4     public String getName() {return name;}
     5     public void setName(String name) {this. name = name;}
     6     public String getPassword() {return password;}
     7     public void setPassword(String password) {this.password = password;}
     8     public String execute()throws Exception{        
     9         if(getName().equals("scott")&&getPassword().equals("tiger")){
    10             ActionContext.getContext().getSession().put("name", getName());
    11             return "success";
    12         }else{
    13             return "error";
    14         }
    15     }  }
    LoginAction
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
     3     "http://struts.apache.org/dtds/struts-2.0.dtd">
     4 <struts>
     5     <constant name="struts.custom.i18n.resources" value="messageResource" />    
     6     <package name="ch02" extends="struts-default">
     7         <action name=“login" class=“basic.struts.LoginAction">
     8             <result name="input">/login.jsp</result>
     9             <result name="error">/error.jsp</result>
    10             <result name="success">/welcome.jsp</result>
    11         </action>
    12     </package>
    在struts.xml中配置LoginAction
  • 相关阅读:
    JavaScript 数组
    Function类型
    javascript面向对象(一)
    javascript变量的作用域
    登陆验证
    注册验证
    php类
    二叉搜索树的 查询最小值
    二叉 搜索树查找最大值
    二叉搜索树 中查找是否存在该值
  • 原文地址:https://www.cnblogs.com/sunhongyu/p/3714438.html
Copyright © 2011-2022 走看看