struts2 简单注解配置实现用户登录

1. 主要文件 LoginAction.java

package com.edu.struts2.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionSupport;


@Namespace("/struts2_annotaction")  //  相当于xml里的namespace配置
public class LoginAction extends ActionSupport {
 private String username;
 private String password;

 private static final long serialVersionUID = 1L;

 
 @Override
 @Action(results = { @Result(name = "success", location = "/success.jsp"),
   @Result(name = "failure", location = "/failure.jsp") })
 public String execute() throws Exception {
  
  if (username.equals(password))
   return "success";
  else
   return "failure";
 }

 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;
 }

}

 

2. 主要文件2 login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 </head>

 <body>

<!-- 访问的路径的action名字是类名除了Action之外的名字,首字母不大写。如:类LoginAction访问的action路径为login.action -->

  <form
   action="${ pageContext.request.contextPath }/struts2_annotaction/login.action"
   method="post">
   <input type="text" name="username" />
   <br>
   <input type="password" name="password" />
   <br>
   <input type="submit" value="提交" />
  </form>
 </body>
</html>

3. 工程的struts2的jar包使用编译器自动导入的。主要是这个jar:struts2-convention-plugin-2.3.4.1.jar

 

4。 整个流程比起xml配置简单了很多,思路也更容易理解了。并且之前的struts.xml可以删除了!!!可以不用这个配置文件也能实现基本的功能了。。

 

5. 更多的信息可以看这里:

http://struts.apache.org/release/2.1.x/docs/convention-plugin.html#ConventionPlugin-Actionannotation 

6. 你也可以直接访问struts2的官网获取最新的消息。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值