您的位置:首页 > 运维架构 > Tomcat

Struts2.5+eclipse+tomcat8.5配置

2017-05-13 23:08 351 查看

1. 下载struts2.5文件并解压



2. 进入struts-2.5.10.1-all\lib



3.找到下图中的jar包导入到eclipse的web工程下的WIn-INF下的lib文件夹中





4. 配置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>框架测试</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>

<!-- struts配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern><!-- 对-ura用 -->
</filtermpping>

</web-app>


5.action类

package test;

public class HelloStruts{
public String execute() {
System.out.println("执行action");
return "success";
}
}


6. 配置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="test" namespace="" extends="struts-default">
<!-- 包名自定 -->
<action name="hellostruts" class="test.HelloStruts" method="execute">
<!-- name: action名称,在jsp页面用 action的name.action访问 -->
<!-- class: 实现类 -->
<!-- method: 实现的方法 -->
<result name="success"><!-- 结果集 ,根据name的值转到不同的页面 -->
/test.jsp
</result>
</action>
</package>
</struts>


7. 测试页面index.jsp和test.jsp

index.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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>
<s:form action="/hellostruts.action"><!--/actionname . action--!>
<s:submit value="Click Here"></s:submit>
</s:form>
</body>
</html>




test.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>
<h1>Hello Action!</h1>
</body>
</html>




web工程目录结构:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struts2.0 struts web.xml