zoukankan      html  css  js  c++  java
  • struts2 简单注解配置代替xml配置文件

    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(struts注解包)

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

  • 相关阅读:
    怀才就像怀孕,时间久了才能让人看出来
    程序开发基础学习五(json配置、解析文件,c++篇)
    PHP 代码 加密
    2010年8月22日周日_StylingAndSkinningOverview_6.10
    2010年8月21日周六_GeoprocessingTasks_6.7
    2010年8月19日周四_insideTheAPI_Using Extent_6.4
    2010年8月22日周日_StylingAndSkinningNavigation_6.11
    2010年8月20日周五_UsingGraphic_6.5
    2010年8月18日周三_insideTheAPI_usingMap_6.2
    2010年8月21日周六_RoutingTasks_6.8
  • 原文地址:https://www.cnblogs.com/zj233/p/5864374.html
Copyright © 2011-2022 走看看