Struts2之2.5.10配置
struts2开发环境搭建,环境eclipse + struts 2.5.10 + Tomcat 9
1、eclipse下新建web工程
2、将jar包拷贝到WEB-INF/lib下
3、配置struts.xml 和web.xml【这两个文件可以在下载struts2时struts-2.5.10.1-apps中得到,解压这里面的war包在相应位置能找到,包括这里没有写得log4j2.xml】
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="default" extends="struts-default"> <action name="helloWorld" class="com.marost.action.HelloWorldAction" method="excute"> <result name="success">/WEB-INF/page/hello.jsp</result> </action> </package> </struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="starter" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Struts 2 Rest Example</display-name> <!-- Filters --> <!-- START SNIPPET: filter --> <filter> <filter-name>action2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- END SNIPPET: filter --> <filter-mapping> <filter-name>action2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Welcome file lists --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- Restricts access to pure JSP files - access available only via Struts action --> <security-constraint> <display-name>No direct JSP access</display-name> <web-resource-collection> <web-resource-name>No-JSP</web-resource-name> <url-pattern>*.jsp</url-pattern> </web-resource-collection> <auth-constraint> <role-name>no-users</role-name> </auth-constraint> </security-constraint> <security-role> <description>Don't assign users to this role</description> <role-name>no-users</role-name> </security-role> </web-app>
4、新建action
package com.marost.action; public class HelloWorldAction { private String msg; public String getMessage() { return msg; } public String excute(){ msg = "你好!"; return "success"; } }
5、新建jsp
在body中输出action中的内容
<%@ 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> ${message } </body> </html>
最终的工程结构如下:
访问:http://localhost:8080/Struts2/helloWorld