`
niulanshan
  • 浏览: 565380 次
文章分类
社区版块
存档分类
最新评论

配置Struts2的过程(样例)

 
阅读更多

1、首先,打开MyEclipse开发软件,新建一个Web Project,命名struts2;


2、配置Tomcat服务器



3、配置server.xml

server.xml:

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8005" shutdown="SHUTDOWN">
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the JSSE configuration, when using APR, the
         connector should be using the OpenSSL style configuration
         described in the APR documentation -->
    <!--
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t "%r" %s %b" />
               
       <Context path="/struts2" docBase="D:\MyEclipse\workspace\struts2\WebRoot" reloadable="true"/>

      </Host>
    </Engine>
  </Service>
</Server>


4、打开struts2,在WebContent目录下新建一个login.jsp文件,源码如下:

login.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>登录界面</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<style type="text/css">
	 fieldset{
	      font-size: 12px;
	      font-weight: bold;
	      background-color: #807989;
	      text-align: center;
	      width:240px;
	      height: 100px;
	 }
	    form{
	      font-size:12px;
	      text-align:center;
	      background-color: #CCCCCC;
	      width:240px;
	      height: 90px;
	    }
	</style>

  </head>
  
  <body>
   <center>
    <fieldset>
         <legend>登录界面</legend>
         <form action="login.action" method="post">
                       用户名:<input type="text" name="username" id="username" value=""/><br/>
                       密 码:<input type="password" name="password" id="password" value=""/><br/>
         <input type="submit" name="submit" value="提交">
         <input type="reset" name="reset" value="重置">
     </form>
    </fieldset>
   </center>
  </body>
</html>

5、配置web.xml

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
	
<struts>
  <!-- 配置名为login的Action -->
  <package name="struts2" extends="struts-default">
        <action name="login" class="com.you.struts.LoginAction">
               <result name="success">/result.jsp</result>
        </action>
  </package>
</struts>

6、导入struts2夹包


7、在src目录下新建一个包,并在该包下创建一个LoginAction类和struts.xml文件


8、编写LoginAction,它的源文件如下:

LoginAction.java:

/**
 * @Title:LoginAction.java
 * @Package:com.you.struts
 * @Description:登录的Action
 * @author:Youhaidong(游海东)
 * @date:2013-6-30 上午12:01:36
 * @version V1.0
 */
package com.you.struts;

/**
 * 类功能说明
 * 类修改者 修改日期
 * 修改说明
 * <p>Title:LoginAction.java</p>
 * <p>Description:游海东个人开发</p>
 * <p>Copyright:Copyright(c)2013</p>
 * @author:游海东
 * @date:2013-6-30 上午12:01:36
 * @version V1.0
 */
public class LoginAction {
	
	//用户名
	private String username;
	//密码
	private String password;
	
	/**
	 * execute方法
	 * @Title:execute
	 * @Description:execute
	 * @param:@return
	 * @param:@throws Exception
	 * @return:String
	 * @throws
	 */
	public String execute() throws Exception{
		return "success";
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}

9、配置struts.xml

struts.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
	
<struts>
  <!-- 配置名为login的Action -->
  <package name="struts2" extends="struts-default">
        <action name="login" class="com.you.struts.LoginAction">
               <result name="success">/result.jsp</result>
        </action>
  </package>
</struts>

10、再次在WebContent目录下新建一个返回结果的result.jsp文件

result.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>返回结果界面</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<style type="text/css">
	   table{
	      font-size: 12px;
	      background-color: #A2BCD8;
	      border:1px solid #E8F2FE;
	   }
	   label{
	      font-size: 12px;
	      font-weight: bold;
	   }
	</style>

  </head>
  
  <body>
     <table>
        <tr>
          <td>
            <label>用户名:${requestScope.username}</label> <br>
            <label>密码:${requestScope.password}</label>
          </td>
        </tr>
     </table>       
  </body>
</html>

11、运行Tomcat,在浏览器地址栏输入:http://localhost:8080/struts2/login.jsp

(1)按回车键,会显示如下图:


(2)输入用户名和密码后,点击“提交”,会跳到:http://localhost:8080/struts2/login.action



分享到:
评论

相关推荐

    struts2入门样例

    这将有助于你理解如何定义Action、配置Struts2框架,以及如何在实际项目中使用Struts2进行开发。 在实践中,你可能还会遇到更多高级特性,如国际化、主题和皮肤、自定义拦截器、异常处理等。随着对Struts2的深入...

    struts2shiro2springhiberante整合样例代码

    1. 配置Struts2:首先,我们需要在项目中引入Struts2的相关库,并配置struts.xml文件,定义Action和Result,以处理用户的请求。 2. 集成Spring:Spring可以通过XML或注解方式管理Bean,将业务对象和DAO注入到Struts...

    struts1+mvn样例程序

    在本样例程序中,我们结合了Struts1与Maven,后者是一个项目管理和集成工具,能够帮助开发者处理构建过程中的依赖管理。 首先,让我们深入理解`pom.xml`文件。这是Maven项目的配置文件,它包含了项目的基本信息,如...

    struts2-2.2.3-all.zip

    通过研究这些样例,开发者可以学习到如何配置Struts2、如何编写Action类、如何使用不同的插件等。 ### Struts2_API Struts2_API文档是开发人员不可或缺的重要资源之一,它详细介绍了Struts2框架中的各个组件、类和...

    javaweb Struts2 框架搭建方法与空框架模型

    - **配置web.xml**:在项目的`WEB-INF/web.xml`中配置Struts2的核心过滤器`StrutsPrepareAndExecuteFilter`,指定Action映射的默认结果类型。 - **编写struts.xml**:这是Struts2的配置文件,用于定义Action、结果...

    struts2框架空白模板.rar

    8. **Web部署描述符(web.xml)**:配置Struts2前端控制器(Filter)和其他Servlets,模板中应包含相应的配置。 9. **测试支持**:为了便于单元测试和集成测试,模板可能包含JUnit或其他测试框架的配置和样例测试类...

    Struts+Spring+Hibernate框架样例+数据库(含增、删、改、查)

    这个"Struts+Spring+Hibernate框架样例+数据库(含增、删、改、查)"的项目,旨在提供一个完整的示例,帮助开发者理解和实践这三大框架的集成使用以及与数据库的交互。 1. **Struts框架**:Struts是基于MVC(Model-...

    Struts2.3.28+spring4.1.6+hibernate4.3.8整合总结(注解)

    - 然后,配置Struts2的核心过滤器,在web.xml中指定`struts2-convention-plugin`,这样Struts2会自动识别注解进行Action配置。 - 接着,配置Spring,创建Spring的配置文件,使用`@Configuration`注解声明这是一个...

    hibernate3.3+spring2+struts2的整合源代码

    标题 "hibernate3.3+spring2+struts2的整合源代码" 提供了我们正在处理一个经典的Java Web应用程序开发案例,其中涉及到三个关键的技术:Hibernate 3.3,Spring 2,以及Struts2。这三者都是开源框架,被广泛用于构建...

    strutsMVC框架

    2. 配置web.xml:配置ActionServlet,并指定Struts的配置文件struts-config.xml的位置。可以参考Struts提供的样例web.xml文件进行配置。 3. 编写JSP页面:创建视图层,使用Struts提供的标签库来与Action Form交互。 ...

    SSH经典框架搭建样例

    3. **配置Struts2**:编写struts.xml配置文件,定义Action类和结果页面。配置过滤器在web.xml中,使Struts2能拦截请求。 4. **配置Spring**:编写applicationContext.xml,声明Bean实例和它们之间的依赖关系。同时...

    SSH框架样例

    1. 配置Struts2:添加Struts2的依赖,配置struts.xml文件,定义Action和结果类型。 2. 集成Spring:引入Spring的依赖,配置spring.xml文件,定义Bean的声明和依赖关系。 3. 配置Hibernate:设置Hibernate的配置文件...

    SSM整合样例

    3. **配置Struts2**:编写struts.xml配置文件,定义Action类与URL的映射,以及拦截器的配置。 4. **配置MyBatis**:创建mybatis-config.xml,设置数据源、事务管理器以及Mapper扫描路径。每个Mapper接口对应的XML...

    struts-portlet 开发

    2. **struts.xml**:这是Struts2的核心配置文件,位于`src/main/resources/META-INF`目录下。该文件定义了Action映射、拦截器配置、结果类型等,对于Struts-Portlet而言,它还负责配置与Portlet的交互逻辑。 #### ...

    SSI框架代码样例

    在学习或使用这个项目时,你需要了解SSI的语法和配置,理解log4j的配置和日志级别,熟悉Struts2的MVC架构和Action配置,以及如何在Java应用中处理JSON数据。同时,阅读readme.txt将帮助你快速上手项目,而深入研究...

    样例代码示例

    标题 "样例代码示例" 暗示了这是一个关于编程实践的集合,特别是针对Spring、Struts2和Hibernate这三大Java企业级开发框架的示例应用。SSH(Spring、Struts2、Hibernate)是Java web开发中常用的“铁三角”,它们...

    ssh+cxf整合及单个cxf测试样例

    这篇内容将深入探讨SSH+CXF的整合过程,以及如何进行单个CXF测试样例。 首先,Spring作为整个应用的依赖注入容器,负责管理所有的Bean。在整合CXF时,Spring可以配置CXF服务的生命周期,包括服务的创建、发布和销毁...

    eclipst.plugin.struts-1.1.5

    2. **配置文件编辑器**:提供了一个图形化的Struts配置文件(struts-config.xml)编辑器,使得配置过程更加直观和易于理解。 3. **代码提示与智能感知**:在编写Struts相关代码时,插件能提供自动补全和错误检查...

    springmvc ssh通用的junit测试样例

    在IT行业中,Spring MVC、SSH(Struts2、Spring、Hibernate)是常见的Java Web开发框架。本示例主要关注的是如何在这些框架下进行单元测试,特别是使用JUnit进行通用的测试实践。单元测试是软件开发过程中的关键部分...

    SpringMVC样例(特详,特简单)

    现代Web应用更倾向于使用Spring Boot,它内置了SpringMVC,并简化了配置过程,同时提供了对各种技术栈的一站式支持,包括数据库连接和ORM。 总之,这个SpringMVC样例项目展示了如何在一个Web应用中有效地结合使用...

Global site tag (gtag.js) - Google Analytics