`
lihongyang66
  • 浏览: 73980 次
  • 性别: Icon_minigender_1
  • 来自: 石家庄
社区版块
存档分类
最新评论

配置struts2

阅读更多

1.在eclipse中新建web项目,命名testJ2EE

2.下载struts2,将struts-2.xx\lib目录下的freemarker-x.jar,ognl-x.jar,struts2-core-x.jar,xwork-x.jar,commons-logging-x.jar,commons-fileupload-x.jar
  拷贝到WebRoot/WEB-INF/lib下(struts2的最下依赖包)

3.在web.xml中配置struts2

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
	
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>	
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

   
4.在src下添加struts2的配置文件struts.xml

<!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" extends="struts-default">
		<action name="testStruts2" class="testJ2EE.test.TestStruts2" method="test">
		  <result name="success">/pages/test_struts2.jsp</result>
		</action>
	</package>
</struts>
 



5.struts2配置完成,添加测试struts2是否配置成功的类
        TestStruts2.java

package testJ2EE.test;

public class TestStruts2 {
	public String test(){
		return "success";
	}
}
 

       

    添加WebRoot/pages/test_struts2.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>success</title>
</head>
<body>
 struts2 整合成功
</body>
</html>
 

   

      添加配置文件struts.xml

<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>	
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>
 

     

6.部署到tomcat,在浏览器中输入http://localhost:8081/testJ2EE/testStruts2.action


成功!!!

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics