Eclipse 搭建Struts2
Eclipse版本 Mars Release (4.5.0)
Struts版本 struts-2.5.20 下载地址:https://struts.apache.org/download.cgi#struts2520
一、创建web项目

命名为MyStruts2

勾选web.xml

二、拷贝struts的jar包
从struts-2.5.20-all\struts-2.5.20\lib拷贝jar文件,复制到WEB-INF\lib文件夹下

然后配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>MyStruts2</display-name> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
路径为:

然后创建struts.xml, 路径为src/struts.xml
<?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">
<!-- START SNIPPET: xworkSample -->
<struts>
<!-- 是否开启动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="com.example.struts2.LoginAction" method="login">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
三、进行测试
1、创建index.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>
<form action="login.action" method="post">
用户名:<input type="text" name="username">
密码:<input type="text" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
2、创建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>
Error!
</body>
</html>
3、创建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>
Success!
</body>
</html>
4、新建一个Servlet类,用来将输入的用户名和密码进行测试,使用户名如果正确跳转到success页面,否则到error页面(继承ActionSupport与否都可以)
package com.example.struts2; import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext; public class LoginAction extends ActionSupport { HttpServletRequest req = ServletActionContext.getRequest();
String username = req.getParameter("username");
String password = req.getParameter("password"); 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;
} public String login(){
if("Larry".equals(username)
&& "123456".equals(password)){
return "result";
}else{
return "error";
}
} }
5、测试接口
1) Success

2)Error

参考:
Eclipse 搭建Struts2的更多相关文章
- Eclipse搭建struts2环境
搭建struts2环境 大的方面分为三步: 1. 加入jar包 2. 在web.xml中配置struts2 3. 添加struts2的配置文件struts.xml 下面是详细步骤: 1. 新建一个Dy ...
- eclipse搭建struts2环境及所遇到的问题
最近几天一直在搭建struts2框架,本身struts2框架的搭建是非常简单的,但不知道为什么最近就是总是报错,报了一大串的错 首先就是每次在类的根路径下创建struts.xml时,就报错,也不知道为 ...
- 用eclipse 搭建struts2环境
一,下载struts2对应的jar包,(http://struts.apache.org/download.cgi#struts2514.1),我一般下载struts2.3版本的 二,打开eclips ...
- Eclipse 搭建struts2 spring3 hibernate3环境实战 待完善
1.struts2 目前是2.3版本,下载地址http://struts.apache.org/download.cgi struts2包 struts2-core-2.3.16.3.jar stru ...
- Eclipse添加struts2
参照:http://jingyan.baidu.com/article/915fc414fd94fb51394b208e.html 一.插件下载:http://struts.apache.org/do ...
- Eclipse搭建SSH(Struts2+Spring+Hibernate)框架教程
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 前言 确实,刚创博客,对于这个陌生的东西还是有些许淡然.这是我的第一篇博文,希望能给你们有帮助,这就是我最大的乐趣! 好了下面进入正题: SS ...
- 使用maven+eclipse搭建最简单的struts2的helloworld
使用maven+eclipse搭建最简单的struts2的helloworld 一.web分层结构简介 1.web[细]粒度分层结构: 按细粒度分层可以分为以下6种: 1).表现层:html/css/ ...
- 用Eclipse搭建ssh框架
问:ssh是哪三大框架,以及他们的作用是什么? 答:分别是struts,spring,hibernate. struts的作用是:是web层,其核心是mvc模式,他可以自动获取参数,自动类型转换,自动 ...
- struts2系列(一):struts2入门(struts2的产生、struts2的工作流程、搭建struts2开发环境)
一. struts2的产生 struts1的缺点: 1. ActionForm过多,而且这个ActionForm在很大程度上又和VO(POJO)重复 ...
随机推荐
- Java面向对象程序设计----接口
接口:接口是一套规范.一个比抽象类更抽象的类. 接口中只能写抽象方法.接口中没有构造函数接口中的变量:public Stratic final接口怎么来使用(implements)实现接口 接口语法: ...
- The Essential Burp Suite
OK we have download teh burp suite .let's begin start the tool 1.if we want to use the total mem ...
- binlog2sql安装
3.pip安装 3.1 首先安装setuptools软件包: (1)下载setuptools包 wget https://pypi.python.org/packages/source/s/setup ...
- springboot rabbitmq 死信队列应用场景和完整demo
何为死信队列? 死信队列实际上就是,当我们的业务队列处理失败(比如抛异常并且达到了retry的上限),就会将消息重新投递到另一个Exchange(Dead Letter Exchanges),该Exc ...
- npm start a http server( 在windows的任意目录上开启一个http server 用来测试html 页面和js代码,不用放到nginx的webroot目录下!!)
原文:https://stackabuse.com/how-to-start-a-node-server-examples-with-the-most-popular-frameworks/#:~:t ...
- P1072 Hankson 的趣味题[数论]
题目描述 Hanks 博士是 BT(Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲解了 ...
- 团队作业Beta冲刺--第二天
团队作业Beta冲刺 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 你们都是魔鬼吗 作业学习目标 (1)掌握软件黑盒测试技术:(2)学会编制软件 ...
- ado.net核心:DataTable对象
概述 DataTable表示内存中数据的一个表. .net命名空间:System.Data DataTable构造方法 DataTable() //不带参数初始化DataTable 类的新实例. Da ...
- Spring源码窥探之:@Value
1. 首先定义实体 /** * @author 70KG * @Title: Apple * @Description: 苹果实体 * @date 2018/10/22下午9:26 * @From w ...
- Oracle 中的 TO_DATE 和 TO_CHAR 函数 日期处理
Oracle 中的 TO_DATE 和 TO_CHAR 函数 oracle 中 TO_DATE 函数的时间格式,以 :: 为例 格式 说明 显示值 备注 Year(年): yy two digits( ...