Struts2环境搭建以及简单测验

下载Struts

直接去官网下载,这里下的是2.3版本的。

创建一个web项目然后加入jar文件

在这里插入图片描述

配置struts.xml文件和web.xml文件

1.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.0.dtd"> 
           
<struts>
	<!-- 
		package:表示包
				name:包名,在Struts.xml文件不能有相同的包名,包名是唯一的
				extends:继承,固定是struts-default
		Action:动作
				name:相当于Servlet的映射路径
				class:处理请求的类,相当于Servlet类
				method:处理请求的方法。
		result:结果,返回jsp页面。
	
	 -->
	 <!--devMode 开发模式  修改一点东西就能给予反馈  -->
	  <constant name="struts.devMode" value="true"/>
	 <package name="p1" extends="struts-default" namespace="/">
	    <action name="hello" class="com.Action.Action" method="sayHello">
	 
	        <result name="success">/success.jsp</result>
	 
	    </action>
	 
	 </package>
</struts>

2.web.xml文件的的位置以及配置内容
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>day01_Struts2_demo</display-name>
 
 <!-- 配置Struts过滤器(拦截所有的请求) -->
  <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>        
    </filter>
    <!-- struts过滤器拦截请求的规则 -->
	<filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  <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>
</web-app>

简单测验

创建Action类,和success.jsp。
在这里插入图片描述

Action类

package com.Action;

public class Action {

	public String sayHello(){
		
		System.out.println("HelloAction的sayHello方法被调用了");
		
		return "success";
		
	}
	
	
}

在这里插入图片描述
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>
由Hello Action的sayhello方法返回的页面
</body>
</html>

运行tomcat并在浏览器中搜索http://localhost:8080/aaa/bbb
aaa:struts.xml文件配置的action属性中的class
bbb: struts.xml文件配置的action属性中的name

发布了47 篇原创文章 · 获赞 5 · 访问量 2048

猜你喜欢

转载自blog.csdn.net/OVO_LQ_Start/article/details/104138722