首页 > 代码库 > Struts2之2.5.10配置
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
Struts2之2.5.10配置
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。