Struts2+Hibernate+Spring框架搭建(一)

  • 一:搭建Struts2的框架 

    1.首先新建一个"WebProject",名称为----FirstSshProject 

    2.将Struts2所需要的Jar包拷贝到“WEB-INF”下面的 lib目录中 
      Struts2所需要的Jar包如下 
     

    3.在src目录下新建包“com.wl.struts.action”,在此包下新建一个名为“loginAction”的类,并且继承“ActionSupport”。重写这个类的“execute()”方法 
    loginAction.java的代码如下: 
    Java代码   收藏代码
    1. package com.wl.struts.action;  
    2.   
    3. import com.opensymphony.xwork2.ActionSupport;  
    4.   
    5. @SuppressWarnings("serial")  
    6. public class loginAction extends ActionSupport {  
    7.   
    8.     @Override  
    9.     public String execute() throws Exception {  
    10.   
    11.         return SUCCESS;  
    12.     }  
    13.   
    14. }  


    4. 
      1)修改WebRoot下面的“index.jsp”文件 
    Java代码   收藏代码
    1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
    2. <%  
    3. String path = request.getContextPath();  
    4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
    5. %>  
    6.   
    7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
    8. <html>  
    9.   <head>  
    10.     <base href="<%=basePath%>">  
    11.       
    12.     <title>My JSP 'index.jsp' starting page</title>  
    13.     <meta http-equiv="pragma" content="no-cache">  
    14.     <meta http-equiv="cache-control" content="no-cache">  
    15.     <meta http-equiv="expires" content="0">      
    16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
    17.     <meta http-equiv="description" content="This is my page">  
    18.     <!--  
    19.     <link rel="stylesheet" type="text/css" href="styles.css">  
    20.     -->  
    21.   </head>  
    22.     
    23.   <body>  
    24.     This is my JSP page. <br>  
    25.     <a href="login.action">test action</a>  
    26.   </body>  
    27. </html>  

    2)在WebRoot目录下面添加success.jsp文件 
    Java代码   收藏代码
    1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
    2. <%  
    3. String path = request.getContextPath();  
    4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
    5. %>  
    6.   
    7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
    8. <html>  
    9.   <head>  
    10.     <base href="<%=basePath%>">  
    11.       
    12.     <title>My JSP 'success.jsp' starting page</title>  
    13.       
    14.     <meta http-equiv="pragma" content="no-cache">  
    15.     <meta http-equiv="cache-control" content="no-cache">  
    16.     <meta http-equiv="expires" content="0">      
    17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
    18.     <meta http-equiv="description" content="This is my page">  
    19.     <!--  
    20.     <link rel="stylesheet" type="text/css" href="styles.css">  
    21.     -->  
    22.   
    23.   </head>  
    24.     
    25.   <body>  
    26.     Success. <br>  
    27.   </body>  
    28. </html>  

    3)在WebRoot目录下面添加error.jsp文件 
    Java代码   收藏代码
    1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
    2. <%  
    3. String path = request.getContextPath();  
    4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
    5. %>  
    6.   
    7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
    8. <html>  
    9.   <head>  
    10.     <base href="<%=basePath%>">  
    11.       
    12.     <title>My JSP 'error.jsp' starting page</title>  
    13.       
    14.     <meta http-equiv="pragma" content="no-cache">  
    15.     <meta http-equiv="cache-control" content="no-cache">  
    16.     <meta http-equiv="expires" content="0">      
    17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
    18.     <meta http-equiv="description" content="This is my page">  
    19.     <!--  
    20.     <link rel="stylesheet" type="text/css" href="styles.css">  
    21.     -->  
    22.   
    23.   </head>  
    24.     
    25.   <body>  
    26.     Error. <br>  
    27.   </body>  
    28. </html>  


    5.在src目录下面新建一个struts.xml的文件 
    Java代码   收藏代码
    1. <?xml version="1.0" encoding="UTF-8" ?>  
    2. <!DOCTYPE struts PUBLIC  
    3.     "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"  
    4.     "http://struts.apache.org/dtds/struts-2.1.7.dtd">  
    5. <struts>  
    6.      
    7.    <package name="login" namespace="/" extends="struts-default">  
    8.        <action name="login" class="com.wl.struts.action.loginAction">  
    9.            <result name="success">/success.jsp</result>  
    10.            <result name="error">/error.jsp</result>  
    11.        </action>  
    12.    </package>  
    13. </struts>  


    6.修改“Web.xml”的配置信息,将Struts2的配置信息添加进去 
    Java代码   收藏代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app version="2.5"   
    3.     xmlns="http://java.sun.com/xml/ns/javaee"   
    4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
    7.   <welcome-file-list>  
    8.     <welcome-file>index.jsp</welcome-file>  
    9.   </welcome-file-list>  
    10.     
    11.   <filter>  
    12.      <filter-name>struts2</filter-name>  
    13.      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
    14.   </filter>  
    15.     
    16.   <filter-mapping>  
    17.      <filter-name>struts2</filter-name>  
    18.      <url-pattern>/*</url-pattern>  
    19.   </filter-mapping>  
    20.     
    21. </web-app>  


    7.使用Tomcat部署项目,启动Tomcat,输入"http://localhost:8080/FirstSshProject_001" 

     
    点击“test action ”连接之后,结果如下: 

     

    经过以上操作步骤,Strut2的环境搭建完毕。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值