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

@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能夠刪除了!!!能夠不用這個配置文件也能實現基本的功能了。。java

相關文章
相關標籤/搜索