配置一个Struts2 的项目,实现访问Action后跳转到zy1.jsp的功能,在(服务器端)Action中输出你的姓名,在zy1.jsp中输出你的学号。
1.eclipse创建web项目
项目结构
新建web项目
File->new->Dynamic Web Project
->给web项目命名(HelloStruts2Action)->next->next
->勾选Generate web.xml deployment descriptor->finish
2.导入jar包
Struts2 jar包下载
个人建议下载struts-2.3.37-all.zip
将下载好的文件解压后打开找到lib文件夹,在lib文件夹里寻找以下9个jar包
3.编写web.xml文件
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4.建立一个Action类
package com.lx.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloStruts2Action extends ActionSupport {
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
System.out.println("我的名字");
return SUCCESS;
}
}
5.新建xml文件
在src下新建一个名为struts.xml的文件
内容为
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="hellostruts2" class="com.lx.action.HelloStruts2Action">
<result>/zy1.jsp</result>
</action>
</package>
</struts>
6.创建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>tilte</title>
</head>
<body>
学号
</body>
</html>
7.运行结果
jsp页面run as
修改地址栏为http://localhost:8080/HelloStruts2/hellostruts2.action后回车控制台显示内容
借鉴学习于此博主文章https://www.cnblogs.com/freeweb/p/5079316.html