SSH三大框架整合配置详细步骤(1)

配置Struts2.0

 

3.1 基础配置

       1)引入Struts必需的五个jar包。下载struts-2.1.6-all.zip解压后,struts-2.1.6lib目录下是struts所有的相关jar包。这么多jar包并不是struts必须得,使用struts只需要把下面五个引入即可,以后用到什么jar包,再引入。放到webroot->webinf->lib下面。

²        commons-logging-1.0.4.jar

²        freemarker-2.3.15.jar

²        Ognl-2.7.3.jar

²        Struts2-core-2.1.8.jar

²        xwork-core-2.1.6.jar

       2)修改WEB-INF下的web.xml文件,增加struts2的配置。增加代码如下:这些配置代码对于struts2是不变的,直接复制到web.xml即可。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>


</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
    3)添加struts配置文件。 在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>
</struts>

好了,struts基本配置完毕,是不是很简单?

现在把工程发布到tomcat上去测试一下,在工程名字上点击右键,选择MyEclipseàAdd and Remove project Deployments,在打开的窗口里,点击Add,选择我们之前配置好的tomcat6服务器,如下图:

图解SSH框架配置步骤 <wbr><wbr><二、配置struts2.0、Hibernate、Spring2.5>
 

发布好了,启动tomcat,如果启动无异常,则说明配置成功。

注意:可能会出现struts-default.xml相关异常,根据提示引入相关jar包。我测试的时候是缺少fileupload相关jar包,于是引入了commons-fileupload-1.2.1.jar。

3.2 配置一个Action

 

下面开始配置一个Action吧,以用户登录为例:

1)首先在webroot下面新建一个登陆页面login.jsp,代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>登录</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
  </head>
  <body>
   <s:form name="form1" action="login.action" method="post">
       <s:textfield  name="username" label="username" ></s:textfield>
       <s:password name="password" label="password" ></s:password>
       <s:submit label="submit"></s:submit>
   </s:form>
   <s:actionerror/>
  </body>
</html>

2)在我们已经建好的struts.xml中来配置登录的action。这里定义登录action的名字为login,配置代码如下:

<?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>
 <package name="struts2" extends="struts-default"  namespace="/">
        <action name="login" class="test.LoginAction">
            <result name="success" type="redirect">/index.jsp</result>
            <result name="input">/login.jsp</result>
            <result name="error">/login.jsp</result>
        </action>
    </package>

</struts>





可能出现的错误: Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
解决办法:  加入  commons-fileupload-1.2.1.jar
3)下面就来编写具体的action类了。代码如下:

package test;
import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport 


public String username;
public String password;

public String execute()
{

if(!username.equals("admin"))
{
super.addFieldError("username", "用户名错误!");
return ERROR;
}


if(!password.equals("001"))
{
        super.addFieldError("password", "密码错误!");
    return ERROR;
    
}
      
return SUCCESS;
}
  
public void validate()
{
if(username==null||username.length()==0)
{
super.addActionError("用户名不能为空");
      
}
if(password==null||password.length()==0)
{
   super.addActionError("密码不能为空");
     
}
 
}

}

4)好了,一个Action就创建完成了,重启tomcat测试一下吧。如果第一次使用struts,你可能你明白上面的代码,以后慢慢学习即可,现在先来看一下效果吧。

打开登录页面http://localhost:8080/工程名字/login,输入正确或错误的用户名和密码,看看有什么提示。


 
原文地址:https://www.cnblogs.com/baoendemao/p/3804817.html