struts2学习笔记之十四:使用注解配置Action(不是和spring集成使用)
Struts2支持使用注解配置Action,减少配置文件的配置
Struts2如果要支持注解配置Action,需要插件的支持,导入插件struts2-convention-plugin-2.1.8.1.jar
项目目录树:

1.导入struts2需要的基本包
2.修改web.xml,让struts2拦截Action
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>s2_1</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>
3.提供实体类User.java
package com.orange.domain;
public class User {
private String userName;
private String 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;
}
}
4.提供Action类LoginAction.java
package com.orange.action; import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.ActionSupport;
import com.orange.domain.User; //默认可以不写
@ParentPackage("struts-default")
//根命名空间,可以不写
@Namespace("/")
//全局配置,如果方法上不指定result,则使用该Result
//@Results({@Result(name="success",location="/success.jsp"),
// @Result(name="error",location="/error.jsp")})
public class LoginAction extends ActionSupport{ private static final long serialVersionUID = 1L;
private User user; public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} //@Action(value="login2")
@Action(value="login2" ,results={@Result(name="success",location="/success.jsp"),
@Result(name="error",location="/error.jsp")})
public String login() throws Exception{
if("admin".equals(this.user.getUserName()) && "admin".equals(this.user.getPassword())){
return SUCCESS;
} return ERROR;
}
}
5.配置struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts>
<constant name="struts.i18n.encoding" value="GBK"/>
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true" /> </struts>
6.提供相关的jsp
login.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!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=GBK">
<title>Insert title here</title>
</head>
<body> <form action="login2" method="post">
User:<input type="text" name="user.userName"><br>
Password:<input type="text" name="user.password"><br>
<input type="submit" value="submit">
</form>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!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=GBK">
<title>Insert title here</title>
</head>
<body>
Success!
</body>
</html>
error.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!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=GBK">
<title>Insert title here</title>
</head>
<body>
Error!
</body>
</html>
7.开始测试

输入admin/admin

配置成功!!!
struts2学习笔记之十四:使用注解配置Action(不是和spring集成使用)的更多相关文章
- VSTO学习笔记(十四)Excel数据透视表与PowerPivot
原文:VSTO学习笔记(十四)Excel数据透视表与PowerPivot 近期公司内部在做一种通用查询报表,方便人力资源分析.统计数据.由于之前公司系统中有一个类似的查询使用Excel数据透视表完成的 ...
- Python学习笔记(十四)
Python学习笔记(十四): Json and Pickle模块 shelve模块 1. Json and Pickle模块 之前我们学习过用eval内置方法可以将一个字符串转成python对象,不 ...
- python3.4学习笔记(二十四) Python pycharm window安装redis MySQL-python相关方法
python3.4学习笔记(二十四) Python pycharm window安装redis MySQL-python相关方法window安装redis,下载Redis的压缩包https://git ...
- Java框架spring 学习笔记(十四):注解aop操作
回见Java框架spring Boot学习笔记(十三):aop实例操作,这里介绍注解aop操作 首先编写一个切入点HelloWorld.java package com.example.spring; ...
- 如鹏网学习笔记(十四)ASP.NET
Asp.net笔记 一.Socket类 进行网络编程的类,可以在两台计算机之间进行网络通讯 过程: 向服务器发送指令: GET /index.html HTTP/1.1 Host:127.0.0.1: ...
- 《机器学习实战》学习笔记第十四章 —— 利用SVD简化数据
相关博客: 吴恩达机器学习笔记(八) —— 降维与主成分分析法(PCA) <机器学习实战>学习笔记第十三章 —— 利用PCA来简化数据 奇异值分解(SVD)原理与在降维中的应用 机器学习( ...
- Android学习笔记(十四)——自定义广播
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 我们除了可以通过广播接收器来接收系统广播, 还可以在应用程序中发送自定义的广播.下面我们来分别试一试发送自定义 ...
- Dynamic CRM 2013学习笔记(十四)复制/克隆记录
经常有这样的需求,一个单据上有太多要填写的内容,有时还关联多个子单据,客户不想一个一个地填写,他们想从已有的单据上复制数据,克隆成一条新的记录.本文将介绍如何克隆一条记录,包括它的子单据以生成一条新的 ...
- JavaScript学习笔记(十四)——对象
在学习廖雪峰前辈的JavaScript教程中,遇到了一些需要注意的点,因此作为学习笔记列出来,提醒自己注意! 如果大家有需要,欢迎访问前辈的博客https://www.liaoxuefeng.com/ ...
随机推荐
- Android通过包名打开第三方应用
import android.content.ComponentName; import android.content.Context; import android.content.Intent; ...
- DNS服务器搭建与配置
DNS服务器搭建与配置目录 1.DNS查询方式 2.DNS服务器类型 3.DNS主要配置文件组 4.name.conf文件配置介绍 5.DNS的资源记录格式 6.DNS服务器和客户端配置 7.简单搭建 ...
- Codeforces620E New Year Tree
挺好的一道题 Description link 给一棵树,每个点有颜色 \(c_i\) 为点权,需要实现以下两种操作: 子树修改颜色(覆盖),查询子树颜色种类 \(n \leq 4 \times 10 ...
- Linux--Centos下搭建Git服务器
参考:http://kimi.it/370.html http://blog.csdn.net/wave_1102/article/details/47779401 开始直接用 yum insta ...
- Junit单元测试、反射、注解
Junit单元测试: * 测试分类: 1. 黑盒测试:不需要写代码,给输入值,看程序是否能够输出期望的值. 2. 白盒测试:需要写代码的.关注程序具体的执行流程. * Junit使用:白盒测试 * 步 ...
- js实际应用
-----------------------------获取select选中的值------------------------- 原生JS获得选中select值 var obj = documen ...
- UML-业务规则
样例:
- Django+ajax 返回json数据挨个显示在页面及页面和后台相互传值
通过Ajax传到后台一个值,根据该值返回数据库表中的某一列的值,然后逐个显示到页面,并且给每个加上超链接,可以进行点击查看详细信息 1.通过Ajax传到后台一个值,红色部分为往Django后台传值,蓝 ...
- ANT下载和配置 IDEA
1.下载地址大全: http://archive.apache.org/dist/ant/binaries/ jdk与ant版本有对应关系,目前知道: jdk1.7与ant1.10不兼容,1.7必须用 ...
- Jupyter_Notebook
TA-lib指标库地址 http://github.com/xingbuxing/TA-Lib-in-chinese 1.Jupter是基于网页端写代码,属于一种交互式的编程,除了在上面写代码之外还可 ...