您的位置:首页 > 编程语言 > Java开发

基于Eclipse搭建SSH框架:第一篇 配置struts2

2016-12-04 10:42 387 查看
SSH是Java web开发中常用的框架,本文将讲解SSH框架的搭建过程。这里使用的SSH分别为struts2.3.31,spring3.2.3以及hibernate4.3.10。

具体下载地址,百度即可。还有搭建SSH框架所需要的JDK,eclipse,tomcat这些软件,它们的安装方法这里就不介绍了。下面介绍具体的搭建步骤:

一、在Eclipse中创建一个web项目并配置好Struts2

    1.在eclipse中点击File->New->Dynamic Web Project,并输入项目名称SSHDemo,然后点击Finish。

    2.配置Struts2,将Struts2需要的jar包复制到WebContent下的WEB-INF下的lib里面。必要的jar包如下:

   


     


    3.在WEB-INF下创建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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>S2SH</display-name>
<filter>
<!-- Filter名字 -->
<filter-name>struts2</filter-name>
<!-- Filter入口 -->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<!-- Filter名字 -->
<filter-name>struts2</filter-name>
<!-- 截获的URL -->
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
</web-app>

   

 

  4.在src下创建com.integration.action包,并在包中创建TestAction类,代码如下:

package com.integration.action;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport{

public String execute() throws Exception{

return "success";
}

}
      正如看到的一样,这个类什么也没做,直接返回success。
   5.在src下创建struts.xml文件,编辑其内容如下:

  

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<!-- 指定struts.xml文件的根元素 -->
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!-- 	<constant name="struts.objectFactory" value="spring" />
-->
<!-- 定义包 -->
<package name="default" namespace="/" extends="struts-default">

<action name="test" class="com.integration.action.TestAction">
<result name="success">/success.jsp</result>
</action>

</package>

</struts>

   6.SSHDemo项目概览

     


       其中index.jsp与success.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>
Hello World!!
</body>
</html>

<%@ 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>
访问成功!!
</body>
</html>


  7.运行SSHDemo项目

         在项目上右键点击Run As->Run on Server

        


         在地址栏输入localhost:8080/SSHDemo/test
        


         到此,Struts2配置完成。下一篇博客介绍Struts2与Spring的整合。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  eclipse ssh 框架 java web