zoukankan      html  css  js  c++  java
  • Struts2入门

    Struts2入门

    一.Struts2环境搭建

    1.1 创建一个Maven项目,可参考 https://www.cnblogs.com/xcn123/p/11234071.html 

    1.2 使用Struts2时必须得导入Struts2的jar

    pom.xml添加struts2-core-2.5.13.jar

     <dependency>
    			<groupId>org.apache.struts</groupId>
    			<artifactId>struts2-core</artifactId>
    			<version>2.5.13</version>
    		</dependency>
    

     1.3在src/main/resources中添加Struts2支持的xml文件以及web.xml

    struts.xml(核心配置文件)

    1 <?xml version="1.0" encoding="UTF-8"?>
    2 <!DOCTYPE struts PUBLIC
    3     "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    4     "http://struts.apache.org/dtds/struts-2.5.dtd">
    5 <struts>
    6     <include file="struts-default.xml"></include>
    7     <include file="struts-base.xml"></include>
    8     <include file="struts-sy.xml"></include>
    9 </struts>

     struts-sy.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
     4     "http://struts.apache.org/dtds/struts-2.5.dtd">
     5 <struts>
     6     <package name="sy" extends="base" namespace="/sy">
     7     <action name="/hello_*_*" class="com.Maven_Struts.web.StrutsAction" method="{1}">
     8     <result name="success">/success.jsp</result>
     9     </action> 
    10     </package>
    11 </struts>

    struts-base.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
     4     "http://struts.apache.org/dtds/struts-2.5.dtd">
     5 <struts>
     6     <constant name="struts.i18n.encoding" value="UTF-8" />
     7     <constant name="struts.devMode" value="true" />
     8     <constant name="struts.configuration.xml.reload" value="true" />
     9     <constant name="struts.i18n.reload" value="true" />
    10     <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    11 
    12     <package name="base" extends="struts-default" abstract="true">
    13         <global-allowed-methods>regex:.*</global-allowed-methods>
    14     </package>
    15 </struts>

    配置web.xml

     1 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     2     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     3     version="3.1">
     4     <display-name>Archetype Created Web Application</display-name>
     5     <filter>
     6     <filter-name>struts2</filter-name>
     7     <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
     8     </filter>
     9     <filter-mapping>
    10     <filter-name>struts2</filter-name>
    11     <url-pattern>*.action</url-pattern>
    12     </filter-mapping>
    13 </web-app>

    二.动态方法调用(MVC不具备的优势)

    strutsAction

     struts-sy.xml

    创建一个success.jsp,在页面更换不同的方法名就或得不同的方法

    三.Struts传参的三种方式

    strutsAction部分代码

     1 private User user1 = new User();
     2     private User user2;
     3     private String sex;
     4     private String uname;
     5     
     6     
     7     
     8     public String getUname() {
     9         return uname;
    10     }
    11 
    12     public void setUname(String uname) {
    13         this.uname = uname;
    14     }
    15 
    16     public User getUser2() {
    17         return user2;
    18     }
    19 
    20     public void setUser2(User user2) {
    21         this.user2 = user2;
    22     }
    23 
    24     public String getSex() {
    25         return sex;
    26     }
    27 
    28     public void setSex(String sex) {
    29         this.sex = sex;
    30     }
    31 
    32     public String add() {
    33         System.out.println("默认被调用方法");
    34         return "success";
    35     }
    36     
    37     public String del() {
    38         System.out.println("del方法");
    39         return "success";
    40     }
    41     
    42     public String edit() {
    43         System.out.println("edit方法");
    44         return "success";
    45     }
    46     
    47     public String list() {
    48         System.out.println("list方法");
    49         System.out.println("user1:"+user1);
    50         System.out.println("user2:"+user2);
    51         System.out.println("sex:"+sex);
    52         System.out.println("uname:"+uname);
    53 }

    jsp界面

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 <h3>Struts传参的三种方式</h3>
    11 <a href="${pageContext.request.contextPath }/sy/hello_list.action?uid=001&&uname=zs">测试Modeldrive接口传参</a>
    12 <a href="${pageContext.request.contextPath }/sy/hello_list.action?sex=nan">测试Set接口传参</a>
    13 <a href="${pageContext.request.contextPath }/sy/hello_list.action?user2.uid=002&&user2.uname=ls">测试参数名.属性名接口传参</a>
    14 <h2>steut与J2EE容器的交换</h2>
    15 </body>
    16 </html>

    3.1 set传参


    3.2 参数名.属性名传参


    3.3 实现modeldrive接口 传参

    四.Struts与J2EE容器交互

     4.1 request 传值

    4.1.1 非注入 HttpServletRequest request = ServletActionContext.getRequest();

    4.1.2 耦合

    实现模型驱动接口  调用 ServletActionContext.getRequest()方法

     1 public String list() {
     2         System.out.println("list方法");
     3         System.out.println("user1:"+user1);
     4         System.out.println("user2:"+user2);
     5         System.out.println("sex:"+sex);
     6         System.out.println("uname:"+uname);
     7         //非注入
     8         //耦合
     9         HttpServletRequest request = ServletActionContext.getRequest();
    10         request.setAttribute("rs", user1);
    11         
    12         return "success";
    13     }

    2、值栈传值(get方法传值 ) 自动会将传过来的值赋值

    1 public String list() {
    2         System.out.println("list方法");
    3         System.out.println("user1:"+user1);
    4         System.out.println("user2:"+user2);
    5         System.out.println("sex:"+sex);
    6         return "success";
    7     }

    4.2 注入

    4.2.1 耦合

    实现ServletRequestAware与ServletResponseAware接口  并重写setServletRequest与setServletResponse

     1 public class StrutsAction implements ModelDriven<User>,ServletRequestAware{ 

    定义HttpServletResponse Responses 与 HttpServletRequest request

     1 private HttpServletResponse Responses; 2 private HttpServletRequest request; 

    重写setServletRequest与setServletResponse

    1 @Override
    2     public void setServletResponse(HttpServletResponse Responses) {
    3         // TODO Auto-generated method stub
    4         this.Responses = Responses;
    5     }
    1 @Override
    2     public void setServletRequest(HttpServletRequest request) {
    3         // TODO Auto-generated method stub
    4         this.request = request;
    5         
    6     }
     1     public String list() {
     2         System.out.println("list方法");
     3         System.out.println("user1:"+user1);
     4         System.out.println("user2:"+user2);
     5         System.out.println("sex:"+sex);
     6         System.out.println("uname:"+uname);
     7         //非注入
     8         //耦合
     9 //        HttpServletRequest request = ServletActionContext.getRequest();
    10 //        request.setAttribute("rs", user1);
    11         //解耦
    12 //        ActionContext context = ActionContext.getContext();
    13 //        context.get("权限匿名");        
    14         this.request.setAttribute("rs", user1);
    15         return "success";
    16     }
  • 相关阅读:
    《应用Yii1.1和PHP5进行敏捷Web开发》学习笔记(转)
    YII 小模块功能
    Netbeans代码配色主题大搜集
    opensuse 启动巨慢 解决方法 90s多
    opensuse 安装 网易云音乐 rpm netease music
    linux qq rpm deb opensuse
    openSUSE 安装 alien
    第一行代码 Android 第2版
    Android Studio AVD 虚拟机 联网 失败
    docker error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.29/containers/json: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuratio
  • 原文地址:https://www.cnblogs.com/xcn123/p/11239370.html
Copyright © 2011-2022 走看看