一个简单的struts2配置
目录
1 需求
首先进入一个jsp页面,点击链接提交到Action中,在Action中打印提示信息,之后跳转到另一个jsp页面。
2 需要导入的jar包
本次采用的是struts2-2.3.37,jar包来之struts2-blank.war包中,tomcat自行解压使用
3 项目的目录结构
3.1 demo1.jsp
起始界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/hello.action">struts2入门</a>
</body>
</html>
3.2 success.jsp
跳转页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
跳转成功啦!!!
</body>
</html>
3.3 HelloAction.java
Action
package com.zhujunwei.struts.demo1;
/**
* sturts2入门的Action
* @author zhujunwei
* 2019年4月10日 上午9:35:07
*/
public class HelloAction {
public String execute() {
System.out.println("hello.action执行了。。。");
return "success";
}
}
3.4 struts.xml
配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 配置struts2的常量 -->
<!-- 配置扩展名 -->
<constant name="struts.action.extension" value="action"/>
<!-- Struts2为了管理Action的配置,通过包进行管理 -->
<!-- 配置Struts2的包 -->
<package name="demo1" namespace="/" extends="struts-default">
<!-- 配置action -->
<action name="hello" class="com.zhujunwei.struts.demo1.HelloAction">
<result name="success">/demo1/success.jsp</result>
</action>
</package>
</struts>
3.5 web.xml
需要配置过滤器
<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
id="WebApp_ID" version="4.0">
<display-name>struts2Demo1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>
4 运行结果
---------------
我每一次回头,都感觉自己不够努力,所以我不再回头。
---------------