如果使用struts2,就需要配置文件或者注解,关于struts2的配置文件struts.xml非常熟悉,对于注解可能spring使用的比较多。配置文件的繁琐衬托出了注解的简洁方便,一条或者几条注解解决配置文件几行的内容。

struts2作为控制器建立模型与视图数据的交互。主要对action进行配置。

首先下载struts2-convention-plugin这个jar包,这是使用注解的支持jar包,建议下载最新的版本,可以到struts2的官网上下载最新的全部依赖文件的压缩包

选择需要的依赖文件,注意最新版本的struts2依赖的asm这个jar包也是有要求的,报错了很久在stackoverflow上找到了可靠的解决办法

struts2使用注解还是要使用配置文件配置几条……,在src下新建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>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="false" />
<constant name="struts.i18n.encoding" value="UTF-8"/>
</struts>

新建login.jsp:

<%--
Created by IntelliJ IDEA.
author: DuzhenTong
Date: 2018/1/23
Time: 22:35
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录</title>
</head>
<body>
<form action="/user/login">
用户名:<input type="text" name="name">
密码:<input type="password" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>

success.jsp:

<%--
Created by IntelliJ IDEA.
author: DuzhenTong
Date: 2018/1/23
Time: 22:40
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>欢迎</title>
</head>
<body>
登录成功!!!
</body>
</html>

error.jsp:

<%--
Created by IntelliJ IDEA.
author: DuzhenTong
Date: 2018/1/23
Time: 22:42
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>失败</title>
</head>
<body>
用户名或密码错误!!!
</body>
</html>

web.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<!--注意:使用最新版本的struts2下main的配置和老版本不一致-->
<!--<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>-->
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

在src下新建domain包,新建User类

package domain;

/**
* Created with IDEA
*
* @author DuzhenTong
* @Date 2018/1/23
* @Time 22:29
*/
public class User { private int id;
private String name;
private String password; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} @Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}

新建action包,新建LoginAction类(使用注解的action所在的包名必须是action,actions等等,在官方的文档中有说明,可以参考):

package action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.*; /**
* Created with IDEA
*
* @author DuzhenTong
* @Date 2018/1/23
* @Time 22:30
*/ @ParentPackage("struts-default")
@Namespace("/user") public class LoginAction extends ActionSupport { private String name;
private String password; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} @Action(value="login", results={ @Result(name="success", location="/success.jsp"), @Result(name="error", location="/error.jsp")})
public String login(){
if ("a".equals(name) && "a".equals(password)) {
System.out.println("成功");
return "success";
}
return "error";
} }

上面的注解和配置文件中的配置很像,配置表单的action,返回什么样的字符串跳转到什么页面

整体的结构:

发布项目启动服务器发起访问输入用户名和密码测试

struts2基于注解配置action的更多相关文章

  1. Struts2基于注解的Action配置

    使用注解来配置Action的最大好处就是可以实现零配置,但是事务都是有利有弊的,使用方便,维护起来就没那么方便了. 要使用注解方式,我们必须添加一个额外包:struts2-convention-plu ...

  2. struts2基于注解的action

    使用注解来配置Action的最大好处就是可以实现零配置,但是事务都是有利有弊的,使用方便,维护起来就没那么方便了. 要使用注解方式,我们必须添加一个额外包:struts2-convention-plu ...

  3. Struts2利用注解实现action跳转

    使用注解来配置Action的最大好处就是可以实现零配置,但是事务都是有利有弊的,使用方便,维护起来就没那么方便了. 要使用注解方式,我们必须添加一个额外包:struts2-convention-plu ...

  4. struts2学习笔记之十四:使用注解配置Action(不是和spring集成使用)

    Struts2支持使用注解配置Action,减少配置文件的配置 Struts2如果要支持注解配置Action,需要插件的支持,导入插件struts2-convention-plugin-2.1.8.1 ...

  5. struts2的注解配置全面解析

    以前在用struts2的注解配置时总是要在web.xml中配置一个初始化参数(actionPackages),最近发现不灵了,仔细研究了下发现即使不用在web.xml中配置也能成功,但时灵时不灵的,很 ...

  6. Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用

    Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...

  7. Struts2的使用注解配置Action(零配置)

    1.首先引入struts2注解的jar包:struts2-convention-plugin.jar ------------------------------第一种方式-------------- ...

  8. 转载 - Struts2基于XML配置方式实现对action的所有方法进行输入校验

    出处:http://www.cnblogs.com/Laupaul/archive/2012/03/15/2398360.html http://www.blogjava.net/focusJ/arc ...

  9. spring启动,spring mvc ,要不要xml配置,基于注解配置

    老项目是09-11年搞的,用的是spring+struts2,没有用注解,全xml配置.web.xml中也配置了一大堆. 现在启动新项目,在项目中用spring+springmvc ,主要用注解,也用 ...

随机推荐

  1. tcp/ip 卷一 读书笔记(5)arp和rarp 同网段和不同网段之间的通信过程

    arp和rarp 同网段和不同网段之间的通信过程 IPv6中已经没有arp rarp协议,所以这里都是IPv4. 链路层使用以太网地址来确定目的地址,应用则常使用ip地址通信 arp协议是指从ip地址 ...

  2. Windows10系统故障检测你知道多少-上海IT33

    Windows 10作为微软公司最新的一款操作系统,从使用的方便和界面的整洁上来说,固然是很好的,但是其因为隐私问题,致使很多人不惜一切代价想要远离Windows 10这款操作系统.尽管Windows ...

  3. 笔记+R︱信用风险建模中神经网络激活函数与感知器简述

    每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 本笔记源于CDA-DSC课程,由常国珍老师主讲 ...

  4. CPLD/FPGA厂商概述 .

    随着可编程逻辑器件应用的日益广泛,许多IC制造厂家涉足PLD/FPGA领域.目前世界上有十几家生产CPLD/FPGA的公司,最大的三家是:ALTERA,XILINX,Lattice,其中ALTERA和 ...

  5. error: No curses/termcap library found的解决办法

    mysql版本:5.1.30 已经不记得这次是第几次安装mysql了,遇到这个问题倒是第一次. 之前在tar,./configure,make,make install 经典四步时,从来没有想过其中的 ...

  6. mysql学习笔记03 mysql数据类型

    数值型:整数型 小数型字符串型时间和日期类型 数值型①整数型1 2 3 4 81bin表示1位,1Byte表示一个字节1B=8b.1汉字=2字节(1 word = 2 byte)1字节=8位(1 by ...

  7. R语言︱常用统计方法包+机器学习包(名称、简介)

    一.一些函数包大汇总 转载于:http://www.dataguru.cn/thread-116761-1-1.html 时间上有点过期,下面的资料供大家参考基本的R包已经实现了传统多元统计的很多功能 ...

  8. 图像处理------Fuzzy C Means的聚合算法

    Fuzzy C-Means聚合算法在图像分割(segmentation)和图像视觉处理中常常被用到聚合算法之 一本文是完全基于JAVA语言实现Fuzzy C-Means聚合算法,并可以运用到图像处理中 ...

  9. Tomcat中的Context.xml的<Loader delegate="true"/>

    Tomcat中的Context.xml的<Loader delegate="true"/> 1.<Loader delegate="true" ...

  10. 升级adb注意事项

    最近使用adb devices老提示设备offline,百度试了好多方法都不行,后面才发现是因为没有把adb文件的名称中含有adb的所有文件复制到 c:/windows/system目录,复制完后只记 ...