IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> struts的初步使用 -> 正文阅读

[Java知识库]struts的初步使用

一.struts的配置

第一步:jar的相关依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.wmy</groupId>
  <artifactId>T269_struts</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>T269_struts Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>

//新添加的依赖
   <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
          <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.5.13</version>
        </dependency>
  </dependencies>
  <build>
    <finalName>T269_struts</finalName>
    <plugins>
    <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
    </plugins>
  </build>
</project>

第二步: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>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>

第三步:框架配置文件?

将struts.xml? ???struts-base.xml? ??struts-sy.xml 加到src/main/resources中

?

//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" namespace="/sy">
    <action name="/demo1_*" class="com.pjl.one.web.Demo1Action" method="{1}">
        <result name="bookEdit">/bookEdit.jsp</result>
    </action>
    
    <!-- <action name="/demo1_*_*" class="com.pjl.one.web.Demo1Action" method="{1}">
        <result name="{2}">/{2}.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>
<!-- 引入了struts的默认配置 -->
    <include file="struts-default.xml"></include>
<!-- 开发人员对struts框架的基本配置 -->    
    <include file="struts-base.xml"></include>
<!--项目涉及到的模块分文件管理  -->
    <include file="struts-sy.xml"></include>
</struts>

二.struts的动态方法调用

第一种调用方式

//  struts-sy.xml 中

<package name="sy" extends="base" namespace="/sy">
      <action name="/demo1_*" class="com.dhm.Action.Demo1Action" method="{1}">
      <result name="bookEdit">/bookEdit.jsp</result>
    </action>
    </package>

?新建一个页面为demo1.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>
<h3>struts的动态方法调用</h3>    
<a href="${pageContext.request.contextPath }/sy/demo1_add.action">新增</a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_edit.action">修改</a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_del.action">删除</a><br>

</body>
</html>

第二种调用方式?

? <!-- 第一个*表示调用后台哪个方法,第二个*表示调用方法后返回哪个页面 -->
? ? ? <action name="/demo1_*_*" class="com.dhm.Action.Demo1Action" method="{1}">
? ? ? <result name="{2}">/{2}.jsp</result>
? ? </action>

<a href="${pageContext.request.contextPath }/sy/demo1_del_bookEdit.action">删除</a>??

三.struts的传参

新建实体类

package com.wmy.one.entity;

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

?实现模型驱动接口

package com.wmy.one.web;

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 com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.wmy.one.entity.User;

public class Demo1Action extends ActionSupport implements ModelDriven<User>,ServletRequestAware,ServletResponseAware{
private HttpServletRequest req;
private HttpServletResponse resp;
private User user1=new User();
private String sex;
private User user2;

    public User getUser1() {
    return user1;
}

public void setUser1(User user1) {
    this.user1 = user1;
}

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() throws Exception {
        System.out.println("add===");
        return "bookEdit";
    }
    
    public String del() throws Exception {
        System.out.println("del===");
        return "bookEdit";
    }
    
    public String edit() throws Exception {
        System.out.println("edit===");
        return "bookEdit";
    }

    public String list() throws Exception {
        System.out.println("list===");
        System.out.println(user1);
        System.out.println(sex);
        HttpServletRequest request=ServletActionContext.getRequest();
        request.setAttribute("age",12);
        req.setAttribute("hobby","zhuiju");
        return "bookEdit";
    }
    
    public User getModel() {
        return user1;
    }

    
}

demo1.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>
<h3>struts的动态方法调用</h3>    
<a href="${pageContext.request.contextPath }/sy/demo1_add.action">新增</a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_edit.action">修改</a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_del.action">删除</a><br>


<h3>struts的动态方法调用</h3>    
<a href="${pageContext.request.contextPath }/sy/demo1_list.action?uid=001&&uname=zs">ModelDriver传参</a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_list.action?sex=nv">set方法传参</a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_del.action?user1.uid=002&&uname=xp">对象属性传参1</a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_del.action?user2.uid=002&&user2.uname=xp">对象属性传参2</a><br>

</body>
</html>

展示效果:

?

四.set属性传参?

在子控制器新建一个sex

package com.wmy.one.web;

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 com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.wmy.one.entity.User;

public class Demo1Action extends ActionSupport implements ModelDriven<User>,ServletRequestAware,ServletResponseAware{
private HttpServletRequest req;
private HttpServletResponse resp;
private User user1=new User();
private String sex;
private User user2;

    public User getUser1() {
    return user1;
}

public void setUser1(User user1) {
    this.user1 = user1;
}

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() throws Exception {
        System.out.println("add===");
        return "bookEdit";
    }
    
    public String del() throws Exception {
        System.out.println("del===");
        return "bookEdit";
    }
    
    public String edit() throws Exception {
        System.out.println("edit===");
        return "bookEdit";
    }

    public String list() throws Exception {
        System.out.println("list===");
        System.out.println(user1);
        System.out.println(sex);
        HttpServletRequest request=ServletActionContext.getRequest();
        request.setAttribute("age",12);
        req.setAttribute("hobby","zhuiju");
        return "bookEdit";
    }
    
    public User getModel() {
        return user1;
    }

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

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

<a href="${pageContext.request.contextPath }/sy/demo1_list.action?sex=nv">set方法传参</a><br>?

?展示效果:

五.通过对象属性传参?

新建一个对象

package com.wmy.one.entity;

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

?<a href="${pageContext.request.contextPath }/sy/demo1_list.action?user1.uid=002&&uname=xp">对象属性传参1</a><br>?

?

六.struts与tomCat的集成

第一种:

?public String list() throws Exception {
? ? ? ? System.out.println("list===");
? ? ? ? System.out.println(user1);
? ? ? ? System.out.println(sex);
? ? ? ? HttpServletRequest request=ServletActionContext.getRequest();
? ? ? ? request.setAttribute("age",12);
? ? ? ? req.setAttribute("hobby","zhuiju");
? ? ? ? return "bookEdit";
? ? }

?在bookEdit中调用

<%@ 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>
成功界面======
${age}
</body>
</html>

展示效果:

?

?第二种:

实现ServletRequestAware,ServletResponseAware接口

?

package com.wmy.one.web;

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 com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.wmy.one.entity.User;

public class Demo1Action extends ActionSupport implements ModelDriven<User>,ServletRequestAware,ServletResponseAware{
private HttpServletRequest req;
private HttpServletResponse resp;
private User user1=new User();
private String sex;
private User user2;

    public User getUser1() {
    return user1;
}

public void setUser1(User user1) {
    this.user1 = user1;
}

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() throws Exception {
        System.out.println("add===");
        return "bookEdit";
    }
    
    public String del() throws Exception {
        System.out.println("del===");
        return "bookEdit";
    }
    
    public String edit() throws Exception {
        System.out.println("edit===");
        return "bookEdit";
    }

    public String list() throws Exception {
        System.out.println("list===");
        System.out.println(user1);
        System.out.println(sex);
        HttpServletRequest request=ServletActionContext.getRequest();
        request.setAttribute("age",12);
        req.setAttribute("hobby","zhuiju");
        return "bookEdit";
    }
    
    public User getModel() {
        return user1;
    }

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

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

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-10-19 11:44:20  更:2021-10-19 11:45:25 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 21:43:40-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码