码迷,mamicode.com
首页 > 其他好文 > 详细

struts2--入

时间:2019-07-24 20:52:48      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:ctrl   vat   system   struct   util   实现   交互   ppi   namespace   

   

    Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。

struts的初步使用

  1、创建一个maven项目

   1.1Java代码

技术图片

1.1.1  User.java

 1 package com.yuan.entity;
 2 
 3 public class User {
 4 
 5     private String uid;
 6     private String uname;
 7     
 8     public String getUid() {
 9         return uid;
10     }
11     
12     public void setUid(String uid) {
13         this.uid = uid;
14     }
15     
16     public String getUname() {
17         return uname;
18     }
19     
20     public void setUname(String uname) {
21         this.uname = uname;
22     }
23 
24     @Override
25     public String toString() {
26         return "User [uid=" + uid + ", uname=" + uname + "]";
27     }
28 
29     public User(String uid, String uname) {
30         super();
31         this.uid = uid;
32         this.uname = uname;
33     }
34 
35     public User() {
36         super();
37         // TODO Auto-generated constructor stub
38     }
39     
40     
41     
42 }

1.1.2  HelloAction.java

package com.yuan.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.yuan.entity.User;

/**
 * 1.动态调用方法(mvc不具备的优势)
 * 2.struts中的传参
 *  1、set传参
 *  2、参数名.属性名传参
 *  3、实现modeldriven接口传参
 *  
 * 3.struts与tomcat的交互
 *   如何将后台的值传到前台去
 *   1、通过request传值
 *   2、值栈传值(get方法传值)
 * @author **
 *
 */
public class HelloAction implements ModelDriven<User>,ServletRequestAware,ServletResponseAware {

    private HttpServletResponse response;
    private HttpServletRequest request;
    private User user1 = new User();
    private User user2;
    private String sex;
    
    public User getUser2() {
        return user2;
    }

    public void setUser2(User user2) {
        this.user2 = user2;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String add() {
        System.out.println("add方法");
        return "success";
    }
    
    public String edit() {
        System.out.println("edit方法");
        return "success";
    }
    
    public String del() {
        System.out.println("del方法");
        return "success";
    }
    
    public String list() {
        System.out.println("list方法");
        System.out.println("user1:"+user1);
        System.out.println("user2:"+user2);
        System.out.println("sex:"+sex);
        //非注入--耦合型
//        HttpServletRequest request = ServletActionContext.getRequest();
//        request.setAttribute("rs", user1);
        this.request.setAttribute("rs",user1);
        return "success";
    }

    @Override
    public User getModel() {
        // TODO Auto-generated method stub
        return user1;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request=request;
        
    }

    @Override
    public void setServletResponse(HttpServletResponse response) {
        this.response=response;
        
    }

    
}

 

1.2导入的外部xml文件

技术图片

1.2.1  pom.xml

在pom.xml中插入此段代码


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

 

1.2.2  struts-base.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>
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.configuration.xml.reload" value="true" />
    <constant name="struts.i18n.reload" value="true" />
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />

    <package name="base" extends="struts-default" abstract="true">
        <global-allowed-methods>regex:.*</global-allowed-methods>
    </package>
</struts>

1.2.3  struts-sy.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="sy" extends="base" namespace="/sy">
      <action name="/hello_*" class="com.yuan.web.HelloAction" method="{1}">
        <result name="success">/success.jsp</result>
      </action>
    </package>
</struts>

1.2.4  struts.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>
    <include file="struts-default.xml"></include>
    <include file="struts-base.xml"></include>
    <include file="struts-sy.xml"></include>
</struts>

1.2.5  web.xml

<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">
  <display-name>Archetype Created Web Application</display-name>
  <filter>
    <filter-name>struts2</filter-name>
//ctrl+shift+t 搜索StrutsPrepareAndExecuteFilter,点开第一个,右键拷出全路径名粘贴到此处 <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> </web-app>

 

1.3新建jsp

 技术图片

1.3.1  demo1.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 <h2>struts传参的三种方式</h2>
11 <a href="${pageContext.request.contextPath }/sy/hello_list.action?uid=001&&uname=zs">测试modeldriven接口传参</a>&nbsp;&nbsp;
12 <a href="${pageContext.request.contextPath }/sy/hello_list.action?sex=nv">测试set传参</a>&nbsp;&nbsp;
13 <a href="${pageContext.request.contextPath }/sy/hello_list.action?user2.uid=002&&user2.uname=ls">测试参数名.属性名传参</a>
14 <h2>与j2EE容器的交互</h2>
15 </body>
16 </html>

1.3.2  success.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 成功页面:${rs }
11 </body>
12 </html>

 

 2、动态调用方法(mvc不具备的优势)

 2.1  action代码

1     public String add() {
2         System.out.println("add方法");
3         return "success";
4     }
5     
6     public String edit() {
7         System.out.println("edit方法");
8         return "success";
9     }

2.2  jsp代码

<h2>动态调用方法</h2>
<a href="${pageContext.request.contextPath }/sy/hello_add.action">add方法</a>
<a href="${pageContext.request.contextPath }/sy/hello_edit.action">edit方法</a>

后台输出调用的相对应的方法

  3、struts中的传参   

   3.1、实现 modelDrivern接口传参

   3.1.1  action代码   

public String list() {
        System.out.println("list方法");
        System.out.println("user1:"+user1);
        }

   3.1.2  jsp代码

<a href="${pageContext.request.contextPath }/sy/hello_list.action?uid=001&&uname=zs">测试modeldriven接口传参</a>&nbsp;&nbsp;

  后台输出显示:user1:User [uid=001, uname=zs]

   3.2  set传参

   3.2.1  action代码

 

1 public String list() {
2         System.out.println("list方法");
3         System.out.println("sex:"+sex);4         }

  3.2.2  jsp代码

<a href="${pageContext.request.contextPath }/sy/hello_list.action?sex=nv">测试set传参</a>&nbsp;&nbsp;

  后台输出显示:sex:nv

   3.3类实例.属性名

   3.3.1  action代码

1 public String list() {
2          System.out.println("list方法");
3          System.out.println("user2:"+user2);
4          }

  3.3.2  jsp代码

<a href="${pageContext.request.contextPath }/sy/hello_list.action?user2.uid=002&&user2.uname=ls">测试参数名.属性名传参</a>

后台输出显示:user2:User [uid=002, uname=ls]

 4、struts与tomcat的交互

  4.1  request传值,需要实现ModelDriven接口,调用ServletActionContext.getRequest()方法。

如:

public String list() {
        System.out.println("list方法");
        System.out.println("user1:"+user1);
        System.out.println("user2:"+user2);
        System.out.println("sex:"+sex);
        HttpServletRequest request = ServletActionContext.getRequest();
        request.setAttribute("rs", user1);
        return "success";
    }

前台页面显示:成功页面:User [uid=001, uname=zs]

 

4.2  值栈传值(get方法传值),不需要操作,因为值栈传值会将传过来的值自动赋值

public String list() {
        System.out.println("list方法");
        System.out.println("user1:"+user1);
        System.out.println("user2:"+user2);
        System.out.println("sex:"+sex);
        return "success";
    }

前台页面显示:成功页面:User [uid=002, uname=ls]

5、注入与非注入

5.1  注入,注入需要实现ServletRequestAware接口,重写需要实现的方法setServletRequest;

  将setServletRequest方法的参数私有化作为属性

private HttpServletRequest request;

  setServletRequest方法

@Override
    public void setServletRequest(HttpServletRequest request) {
        this.request=request;
        
    }

属性调用

public String list() {
        System.out.println("list方法");
        System.out.println("user1:"+user1);
        System.out.println("user2:"+user2);
        System.out.println("sex:"+sex);
        this.request.setAttribute("rs",user1);
        return "success";
    }

5.2  非注入

request传值就是属于非注入

public String list() {
        System.out.println("list方法");
        System.out.println("user1:"+user1);
        System.out.println("user2:"+user2);
        System.out.println("sex:"+sex);
        HttpServletRequest request = ServletActionContext.getRequest();
        request.setAttribute("rs", user1);
        return "success";
    }

谢谢观看^-^ !!!

 

struts2--入

标签:ctrl   vat   system   struct   util   实现   交互   ppi   namespace   

原文地址:https://www.cnblogs.com/ly-0919/p/11239371.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!