栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Struts的初步使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Struts的初步使用

一.Struts的配置 新建maven项目

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

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

 

 修改web.xml由2.3至3.1

    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

    
            javax.servlet
            javax.servlet-api
            4.0.0
            provided
        

 
            org.apache.struts
            struts2-core
            2.5.13
        

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

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

 最后代码如下


    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">
    Archetype Created Web Application
    
        struts
        org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
    

    
        struts
        *.action
    


 

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

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

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

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

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


    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

    
        
    

maven项目的结构

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

                              resources  放配置的地方

                              webapp放页面的地方

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

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

二.Struts的动态方法调用 xml配置


    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

    
                     method="{1}">
            /bookEdit
        
        
    

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

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

第二个*的写法基本不用

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




Insert title here


成功界面

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


    struts动态方法调用
    新增
    

    修改
    

    删除
    



 运行结果如图

 

三.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 {
    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"%>




Insert title here


    struts动态方法调用
    新增
    

    修改
    

    删除
    

Struts的传参
ModelDriver传参
    

    set方法传参
    

    通过对象属性传参
    


运行如下

 

2.set方法传参

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

在list方法中打印sex

Demo.jsp界面传值

ModelDriver传参

运行能拿到sex的值

3.通过对象属性传参

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

在list方法中打印user2

Demo.jsp界面传值

通过对象属性传参

打印能取到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界面接收,运行结果在界面会有    小宝

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/324883.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号