struts的初步使用

一、struts的配置

1.导入jar包(maven的pom.xml)

pom.xml文件中

<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.sjy</groupId>
	<artifactId>struts</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>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>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>

2.做配置(配置过滤器)

web.xml中

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" 
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>

框架配置文件

3.开发(写子控制器继承ActionSupport)

package com.sjy;

import com.opensymphony.xwork2.ActionSupport;

public class Demo1Action extends ActionSupport{
	
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()...");
	return "bookEdit";
}
}

做配置

	<action name="/demo1_*" class="com.sjy.Demo1Action" method="{1}">
	<result name="bookEdit">/bookEdit.jsp</result>
	</action>

 {1}代表第一个*

第一个*代表调用方法

第二个*代表跳哪个界面(一般使用一个*的写法)

两个*的写法如下

	<action name="/demo1_*_*" class="com.sjy.Demo1Action" method="{1}">
	<result name="{2}">/{2}.jsp</result>
	</action>

<a href="${pageContext.request.contextPath }/sy/demo1_edit_bookEdit.action">修改</a> 

二、struts动态方法的调用

建立demo.jsp检验

 

点击增加

 控制台出现add()...

 说明配置成功

三、struts的传参

1.Modeldriver接口传参

建立实体类user

package com.sjy.entity;

public class User {
private String uid;
private String uname;
@Override
public String toString() {
	return "User [uid=" + uid + ", uname=" + 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;
}
}

在子控制器内写list方法

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

demo.jsp传参

<a href="${pageContext.request.contextPath }/sy/demo1_list.action?uid=111&uname=sjy">model接口</a>

界面为

点击model接口

控制台为

传参成功 

2.set方法传参

demo.jsp界面传参

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

由于实体类中没有sex属性

在子控制器内添加sex属性和set/get方法

 在list方法中打印sex

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

控制台内容为 

 传参成功

3.通过对象属性传参

在子控制器内写入一个user属性,然后get/set方法 

demo.jsp界面传值

<a href="${pageContext.request.contextPath }/sy/demo1_list.action?user2.uid=222&user2.uname=xxx">对象属性</a> 

 list方法内打印user2

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

控制台结果为 

传参成功 

四、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", "sjy");
	return "bookEdit";
}

将name传到bookEdit.jsp界面中

界面内容为 

 传参成功

第二种方法

实现接口ServletRequestAware并重写方法

将HttpServletRequest定义在方法外

便可实现传参

req.setAttribute("age", 111111);
	return "bookEdit";

bookEdit.jsp内容为

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
192.168.85.1 - - [26/Jun/2022:06:07:07 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 24 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 24 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 24 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 200 12925 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 200 12925 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 200 12925 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 200 14 192.168.85.1 - - [26/Jun/2022:06:08:06 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 15 192.168.85.1 - - [26/Jun/2022:06:08:16 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 1227 192.168.85.1 - - [26/Jun/2022:06:10:15 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 79 192.168.85.1 - - [26/Jun/2022:06:13:25 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 404 752 192.168.85.1 - - [26/Jun/2022:06:16:42 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 35 192.168.85.1 - - [26/Jun/2022:06:16:57 -0400] "GET //struts2-showcase/hhh.jsp HTTP/1.1" 403 642 192.168.85.1 - - [26/Jun/2022:06:18:55 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 35 192.168.85.1 - - [26/Jun/2022:06:19:02 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 35 192.168.85.1 - - [26/Jun/2022:06:19:09 -0400] "GET //struts2-showcase/hhh1.jsp HTTP/1.1" 403 642 192.168.85.1 - - [26/Jun/2022:06:19:34 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 400 192.168.85.1 - - [26/Jun/2022:06:20:37 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 5 192.168.85.1 - - [26/Jun/2022:06:20:42 -0400] "GET //struts2-showcase/hhh1.jsp HTTP/1.1" 403 642 192.168.85.1 - - [26/Jun/2022:06:20:46 -0400] "GET //struts2-showcase/hhh.jsp HTTP/1.1" 403 642 192.168.85.1 - - [26/Jun/2022:06:20:51 -0400] "GET /struts2-showcase/hhh.jsp HTTP/1.1" 403 642
最新发布
07-12

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值