Struts的初步使用

一.Struts的配置

新建maven项目

如下,有时建项目java文件未显示出来

 我们可以右键build path 将下图标识内容取消勾选,即可出现java文件

 

 修改web.xml由2.3至3.1

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

更改jdk由1.5到1.8

通过配置maven-compiler-plugin插件来改变

即在pom.xml文件的build中添加下图红框中的代码段

选择Project Facets,Dynamic Web Module     2.3改为3.1    Java     1.5改为1.8

勾选Dynamic Web Module ,点击下图红框内容,将 WebContent改为src/main/webapp

将不需要的index.jsp删除,项目到此就不会报错了

导入jar包——>maven的pom.xml

    <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0</version>
            <scope>provided</scope>
        </dependency>

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

做配置——>web.xml配置中央控制器——>配置过滤器

Ctrl+Shift+t,选择全路径名

 最后代码如下

<!-- 修改web.xml由2.3至3.1 -->
<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>
 

maven项目所有配置文件默认放在resources下面

第二个为开发人员对struts框架的基本配置

第三个为项目涉及到的模块,分文件管理

框架配置文件( mvc.xml ) 默认为struts.xml

开发——>写子控制器继承ActionSupport——>写子控制器继承ActionSupport

<?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="" method=""></action>
    </package>
</struts>

maven项目的结构

src ——>main——java  写代码的地方

                              resources  放配置的地方

                              webapp放页面的地方

      ——>test——java  写测试代码的地方

                             resources  放测试代码对应配置的地方

二.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>
    <package name="sy" extends="base" namespace="/sy">
        <action name="demo1_*" class="com.ltf.first.Demo1Action"
            method="{1}">
            <result name="bookEdit">/bookEdit</result>
        </action>
        <!-- <action name="demo1_*_*" class="com.ltf.first.Demo1Action"
            method="{2}">
            <result name="{2}">/{2}.jsp</result>
        </action> -->

    </package>
</struts>

第一个*代表调后台哪个方法

第二个*代表调后台某个方法之后返回哪个界面

第二个*的写法基本不用

写Demo1Action

package com.ltf.first;

import com.opensymphony.xwork2.ActionSupport;

public class Demo1Action extends ActionSupport{
public String add() throws Exception {
    System.out.println("add方法...");
    return "bookEdit";
}
public String edit() throws Exception {
    System.out.println("edit方法...");
    return "bookEdit";
}
public String del() throws Exception {
    System.out.println("del方法...");
    return "bookEdit";
}
public String list() throws Exception {
    System.out.println("list方法...");
    return "bookEdit";
}
}
 

 建立bookEdit.jsp界面和demo.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>
成功界面
</body>
</html>

 

<%@ 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>

 运行结果如图

 

三.Struts的传参

1.ModelDriver传参

Demo1Action 

package com.ltf.first;

import com.ltf.entity.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class Demo1Action extends ActionSupport implements ModelDriven<User> {
    private User user1 = new User();

    public String add() throws Exception {
        System.out.println("add方法...");
        return "bookEdit";
    }

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

    public String del() throws Exception {
        System.out.println("del方法...");
        return "bookEdit";
    }

    public String list() throws Exception {
        System.out.println("list方法...");
        System.out.println(user1);
        return "bookEdit";
    }

    @Override
    public User getModel() {
        return user1;
    }
}
 

 demo.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_edit.action">set方法传参</a>
    <br>
    <a href="${pageContext.request.contextPath }/sy/demo1_del.action">通过对象属性传参</a>
    <br>
</body>
</html>

运行如下

 

2.set方法传参

在Demo1Action 中建立sex属性,并建立get,set方法

在list方法中打印sex

Demo.jsp界面传值

<a href="${pageContext.request.contextPath }/sy/demo1_list.action?uid=001&&uname=zs">ModelDriver传参</a>

运行能拿到sex的值

3.通过对象属性传参

建立private User user2;提供get,set方法

在list方法中打印user2

Demo.jsp界面传值

<a href="${pageContext.request.contextPath }/sy/demo1_list.action?user2.uid=002&&uname=xb">通过对象属性传参</a>

打印能取到Demo.jsp界面传的值

4.Struts与Tomcat的集成

将后台参数传到前台

通过内置类

    public String list() throws Exception {
        System.out.println("list方法...");
        System.out.println(user1);
        System.out.println(sex);
        System.out.println(user2);
        HttpServletRequest request = ServletActionContext.getRequest();
        request.setAttribute("name", "小宝");
        return "bookEdit";
    }

在bookEdit.jsp界面接收,运行结果在界面会有    小宝

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小宝的宝呢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值