struts入门

一、 Struts2的环境搭建

       1.在maven项目的pom.xml中引入Struts的依赖

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

    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>

 

 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" >
    
    <action name="/hello_*" class="com.jt.HelloAction" method="{1}">
      <result name="success">/success.jsp</result>
    </action>
    </package>
</struts>

 

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>

 

在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>
   <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>

 动态调用

实体类user

package com.jtc;

public class User {

    private String uid;
    private String uname;
    public String getUid() {
        return uid;
    }
    public void setUid(String uid) {
        this.uid = uid;
    }
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    @Override
    public String toString() {
        return "User [uid=" + uid + ", uname=" + uname + "]";
    }
    public User(String uid, String uname) {
        super();
        this.uid = uid;
        this.uname = uname;
    }
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }
    
}

 

action

package com.jt;

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

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.omg.PortableInterceptor.SUCCESSFUL;

import com.jtc.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

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

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

    public void setUname(String uname) {
        this.uname = uname;
    }

    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 del() {
        System.out.println("del方法");
        return "success";
    }

    public String edit() {
        System.out.println("edit方法");
        return "success";
    }

    public String list() {
        System.out.println("list方法");
        System.out.println("user1:" + user1);
        System.out.println("user2:" + user2);
        System.out.println("sex:" + sex);
        System.out.println("uname:"+uname);
//        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;
    }

}

 

创建一个JSP页面去测试

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>struts传参的三种方式</h2>
<a href="${pageContext.request.contextPath }/sy/hello_list.action?uid=001&&uname=zs">测试modeldriven接口传参</a>
<a href="${pageContext.request.contextPath }/sy/hello_list.action?sex=nv">测试set接口传参</a>
<a href="${pageContext.request.contextPath }/sy/hello_list.action?user2.uid=002&&user2.uname=ls">测试参数名.属性名接口传参</a>
<h2>与J2EE容器的交互</h2>
</body>
</html>

 

再用一个jsp页面来接受结果

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
成功页面:${rs }
</body>
</html>

 

三种测试打印结果

测试modeldriven接口传参
user1:User [uid=001, uname=zs]
user2:null
sex:null
uname:null

 

测试set传参
user1:User [uid=null, uname=null]
user2:null
sex:nv
uname:null

 

测试参数名.属性名传参
user1:User [uid=null, uname=null]
user2:User [uid=002, uname=ls]
sex:null
uname:null

 

注入与非注入

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

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

首先

public class HelloAction implements ModelDriven<User>,ServletRequestAware{
}

 

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";
    }

 

非注入

    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";
    }

 

posted @ 2019-07-25 17:25  youshutup  阅读(155)  评论(0编辑  收藏  举报