struts2入门

在开始前需要确定一个概念,便于理解:

struts2相当于自定mvc中的action层。

 

1、环境搭建

①导jar包

在web.xml中配置过滤器

注:filter-class 为StrutsPrepareAndExecuteFilter类的全类名。

③ struts.xml
                struts.xml(核心配置文件)

下面是配置的代码

struts.xml

另外两个文件的代码:

struts-sy.xml(此文件相当于自定义mvc中的mvc.xml) struts-base.xml(下面为配置的一些常量)

2、Hello struts!


package com.struts.study.one.demo;
 
public class Demo1Action{
	/**
          *如果在配置文件中没有指定方法,struts会默认匹配执行execute方法
          */
	public String execute() {
		System.out.println("Hello struts");
		return "success";
	}
}

新建一个用来测试的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>
	<a href="${pageContext.request.contextPath}/sy/demo1Action.action">click</a>
</body>
</html>

3、动态调用方法

在action标签的name后拼上 _* , 且为action标签加上method属性

这里没有它规定的execute方法,而是我们自己定义的方法


package com.zking.study.one.demo;
 
public class Demo1Action{
 
	public String add() {
                System.out.println("add");
		return "success";
	}
	public String remove() {
                System.out.println("remove");
		return "success";
	}
}

前端访问


<%@ 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>
	<a href="${pageContext.request.contextPath}/sy/demo1Action_add.action">click</a>
</body>
</html>

4、参数赋值

1. Action中定义属性,并提供get/set方法

package com.zking.study.one.demo;
 
public class Demo1Action{
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String execute() {
		System.out.println(name);
		return "success";
	}
}

与普通传参一样,需要与后台的属性名相等


<%@ 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>
	<a href="${pageContext.request.contextPath}/sy/demo1Action.action?name=张三">click</a>
</body>
</html>

2. 实现ModelDriven接口 ,不需要提供get/set方法


package com.zking.study.one.demo;
 
import org.apache.struts2.ServletActionContext;
 
import com.opensymphony.xwork2.ModelDriven;
import com.zking.study.one.entity.User;
 
public class Demo1Action implements ModelDriven<User>{
        //定义的一个实体类,定义了一个属性uname
	private User user=new User(); 
	public String execute() {
		System.out.println(user);
		return "success";
	}
}
<%@ 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>
	<a href="${pageContext.request.contextPath}/sy/demo1Action.action?uname=张三">click</a>
</body>
</html>

5、与J2EE容器交互

可以参考这篇博客:

https://blog.csdn.net/yjt520557/article/details/83831916

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值