struts2环境配置

看过网上那么多的教程,感觉要么不详细,要么模模糊糊,对于小白来说累觉不爱了,所以我决定自己写一个详细的新手教程

先去下载最新版struts2http://struts.apache.org/

得到压缩文件

然后解压

打开apps,解压struts2-blank.war,进入解压后的文件夹

WEB-INFO-》web.xml,这个文件里面的内容要用到

WEB-INFO-》lib,这个文件夹里面的内容要复制到咱们工程的lib文件夹里

WEB-INFO-》classes-》struts.xml,这是strtus的配置文件,下面要用到

 用eclipse新建Dynamic Web Project

一路next,最后勾选,然后点击finish

编写struts.xml文件

把上面找到的strtus.xml复制到src根目录里面,并打开,只留下struts标签

在web.xml中加入struts2 MVC框架启动配置

把上面找到的web.xml打开,看见里面的<filter>和<filter-mapping>标签了吗?

把<filter>和<filter-mapping>标签复制到咱们建好的工程里面的web.xml里面

最后,写一个小例子,Hello Word!

首先,新建一个hello.jsp文件

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <h3>Hello Word!</h3>
11 </body>
12 </html>

然后写一个Action

 1 package demo;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class HelloAction extends ActionSupport{
 6 
 7     /**
 8      * 
 9      */
10     private static final long serialVersionUID = 1L;
11 
12     @Override
13     public String execute() throws Exception {
14         return SUCCESS;
15     }
16 
17     
18 }

struts.xml文件

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7 
 8     <!-- 开发者模式,出错后提示更详细 -->
 9     <constant name="struts.devMode" value="true"></constant>
10     <!-- struts2的action必须放在一个指定的包空间下,包的名字唯一
11          name:    包名
12          abstract:    抽象包
13          extends:    本包继承的包
14          namespace:    命名空间
15     -->
16     <package name="helloPackage" extends="struts-default" namespace="/">
17         <!-- 一个Action可以被多次映射(只要action配置中的name不同)
18              name:    Action名称
19              class: Action处理类对应的类的路径
20              method: 指定Action中的方法名
21         -->
22         <action name="login" method="execute" class="demo.HelloAction">
23             <!-- 处理结果 
24                  name:    对应Action返回逻辑视图名称,默认为success
25             -->
26             <result name="success">/hello.jsp</result>
27         </action>
28     </package>
29 
30 </struts>

测试一

把咱们的工程添加进服务器启动列表里,启动服务器

打开浏览器,输入http://localhost:8080/struts2-demo/login

测试二

导出工程为war文件,把文件移动到

然后到bin目录里,运行startup.bat

http://localhost:8080/struts2-demo/login  访问成功!

到此,配置结束了,祝各位好运,哈哈,一般出错的话,要么缺少jar包,要么struts.xml写的不对

原文地址:https://www.cnblogs.com/LUA123/p/5374090.html