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

 

 

参考:

1、eclipse下简单配置struts2.5.8

Eclipse 搭建Struts2的更多相关文章

  1. Eclipse搭建struts2环境

    搭建struts2环境 大的方面分为三步: 1. 加入jar包 2. 在web.xml中配置struts2 3. 添加struts2的配置文件struts.xml 下面是详细步骤: 1. 新建一个Dy ...

  2. eclipse搭建struts2环境及所遇到的问题

    最近几天一直在搭建struts2框架,本身struts2框架的搭建是非常简单的,但不知道为什么最近就是总是报错,报了一大串的错 首先就是每次在类的根路径下创建struts.xml时,就报错,也不知道为 ...

  3. 用eclipse 搭建struts2环境

    一,下载struts2对应的jar包,(http://struts.apache.org/download.cgi#struts2514.1),我一般下载struts2.3版本的 二,打开eclips ...

  4. Eclipse 搭建struts2 spring3 hibernate3环境实战 待完善

    1.struts2 目前是2.3版本,下载地址http://struts.apache.org/download.cgi struts2包 struts2-core-2.3.16.3.jar stru ...

  5. Eclipse添加struts2

    参照:http://jingyan.baidu.com/article/915fc414fd94fb51394b208e.html 一.插件下载:http://struts.apache.org/do ...

  6. Eclipse搭建SSH(Struts2+Spring+Hibernate)框架教程

    | 版权声明:本文为博主原创文章,未经博主允许不得转载. 前言 确实,刚创博客,对于这个陌生的东西还是有些许淡然.这是我的第一篇博文,希望能给你们有帮助,这就是我最大的乐趣! 好了下面进入正题: SS ...

  7. 使用maven+eclipse搭建最简单的struts2的helloworld

    使用maven+eclipse搭建最简单的struts2的helloworld 一.web分层结构简介 1.web[细]粒度分层结构: 按细粒度分层可以分为以下6种: 1).表现层:html/css/ ...

  8. 用Eclipse搭建ssh框架

    问:ssh是哪三大框架,以及他们的作用是什么? 答:分别是struts,spring,hibernate. struts的作用是:是web层,其核心是mvc模式,他可以自动获取参数,自动类型转换,自动 ...

  9. struts2系列(一):struts2入门(struts2的产生、struts2的工作流程、搭建struts2开发环境)

    一. struts2的产生 struts1的缺点:                         1. ActionForm过多,而且这个ActionForm在很大程度上又和VO(POJO)重复  ...

随机推荐

  1. 臀部——哑铃&杠铃

  2. SQL Server 字段提取拼音首字母

    目前工作中遇到一个情况,需要将SQL Server中的一个字段提取拼音的首字母,字段由汉字.英文.数字以及“-”构成,百度了一堆,找到如下方法,记录一下,以备后用! 首先建立一个函数 --生成拼音首码 ...

  3. amazeui datepicker日历控件 设置默认当日

    amazeui datepicker日历控件 设置默认当日 背景: 最近在做一个系统的时候,前台需要选择日期,传给后台进行处理,每次都需要通过手动点击组件,选择日期,这样子很不好,所以我想通过程序自动 ...

  4. c#版本23个设计模式

    一.引言 对设计模式的学习,自己的感触还是很多的,因为我现在在写代码的时候,经常会想想这里能不能用什么设计模式来进行重构.所以,学完设计模式之后,感觉它会慢慢地影响到你写代码的思维方式.这里对设计模式 ...

  5. DeferredResult使用方式和场景

    为什么使用DeferredResult? API接口需要在指定时间内将异步操作的结果同步返回给前端时: Controller处理耗时任务,并且需要耗时任务的返回结果时: 当一个请求到达API接口,如果 ...

  6. nginx配置白名单

    配置如下: http模块: http { include mime.types; default_type application/octet-stream; #log_format main '$r ...

  7. 题解 UVa10780

    题目大意 多组数据,每组数据给定两个整数 \(m,n\),输出使 \(n\%m^k=0\) 的最大的 \(k\).如果 \(k=0\) 则输出Impossible to divide. 分析 计数水题 ...

  8. PHP项目部署 Linux 服务器

    一.运行环境 Centos7 x64 lnmp (Linux , Nginx , Mysql , PHP/Python) 二.安装依赖和修改配置 安装Lnmp环境集成包:https://lnmp.or ...

  9. input图片上传并显示查看判断图片类型

    有一个问题:上传一次在上传一次关闭按钮会出现两次,关闭之后还有一个(改好了可以告诉我我在修正过来) <div id="box"> <div class=" ...

  10. dp--01背包,完全背包,多重背包

    背包问题 以下代码 n是物品个数,m是背包容积 物品价值和重量int v[maxn],w[maxn]; 01背包 模板 for(int i = 0; i < n; i++) { for(int ...