Eclipse配置struts-2.3.32开发环境

1,下载struts2jar包下载地址http://struts.apache.org/download.cgi#struts25101


最好下载全部的,如果是初学者单单是练习的也可以只下载基础包,struts-2.3.32-min-lib.zip\

下载之后解压出来


commons-fileupload-1.3.2.jar   文件上传支持类库

commons-io-2.2.jar  处理IO操作的工具类库

commons-lang3-3.2.jar   处理Java基本对象方法的工具类包

commons-logging-1.1.3.jar   log4j日志文件支持库

freemarker-2.3.22.jar   freemarker模板语言支持类库

javassist-3.11.0.GA.jar  (JAVA编程助手)使Java字节码操纵简单。这是一个编辑Java字节码的类库。

ognl-3.0.19.jar  Ognl   表达式语言类库

struts2-core-2.3.32.jar  Struts2的核心类库

xword-core-2.3.32.jar  XWord的核心类库


2,创建Javaweb项目

在Eclipse中选择File-New-Dynamic Web Project创建动态项目:


next ->next->finish   勾选上会自动生成web.xml



3,把上面的jar复制到lib



4,配置过滤器:在web.xml上


  <!-- 配置Struts2事件监听 -->
<!-- struts2 -->
    <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>


5,创建Action类

在src下创建一个Action包



在该包中增加一个test类,实现xwork2的Action接口:




编写测试代码

package com.ly.Action;


import com.opensymphony.xwork2.ActionSupport;


public class test extends ActionSupport {

private static final long serialVersionUID = 1L;

private String userName;


public String execute() throws Exception {


if(userName == null || "".equals(userName)){
return ERROR;
}else{
return SUCCESS;
}
}


public String getUserName() {
return userName;
}


public void setUserName(String userName) {
this.userName = userName;
}


}


6,添加index.jsp,并且把jsp设置为utf-8的格式






编写测试代码:

<%@ 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>
<form action="login.action" method="post">
请输入用户名:<input type="text" name="userName">
<input type="submit" value="提交">
</form>
</body>
</html>



7,添加struts的配置文件:注意不要起名为struts2会有冲突,并且要放在src目录下面




编写struts的配置

<?xml version="1.0" encoding="UTF-8" ?>


<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>


<!-- 声明常量  (开发模式)-->
<constant name="struts.devMode" value="true"></constant>
<!-- 声明包 -->
<package name="struts2" extends="struts-default" namespace="/">
    <!-- 定义action -->
        <action name="login" class="com.ly.Action.test"> 
        <!-- 定义处理成功之后的映射界面 --> 
            <result name="error">error.jsp</result>
<result name="success">success.jsp</result>
        </action>       
    </package>  
</struts> 

8,编写跳转代码页面success.jsp和error.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">
<%@ taglib uri="/struts-tags" prefix="e"%>  
<%@ taglib prefix="s" uri="/struts-tags" %>  
  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>欢迎界面</title>  
</head>  
<body>  
<font color="red">
<e:property value="userName"/>,您好
<br>
欢迎来到本站!
</font> 
</body>  
</html> 



error.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>
<font color="red">
错误:您没有输入姓名!
</font>
</body>
</html>


9,启动项目





在浏览器上输入地址http://localhost:88/struts2/index.jsp 效果如下




  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值