刚刚接触struts2,有点懵懵懂懂,还是习惯于先写代码,然后慢慢来理解其中的思想。
这篇文章主要内容是strusts的环境搭建及通过一个简单的例子来理解到底是怎么使用struts来简化编程的。
1.项目结构如下如,包括必须的包
2.web.xml
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
index.jsp
3.struts.xml
/welcome.jsp
/login.html
/login.html
4.LoginAction.java
package action;
import com.opensymphony.xwork2.ActionSupport;
//该类继承了ActionSupport类。这样就可以直接使用SUCCESS, LOGIN等变量和重写execute等方法
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;
}
/**
* 在这里返回值使用自定义的
* @return
*/
public String index(){
return index;
}
/**
* 这里的返回值直接使用SUCCESS等变量
*/
public String execute(){
//会自动取出页面表单中的值
if(sa.equals(name)&&aaaaaa.equals(password))
return SUCCESS;
return LOGIN;
}
}
6.login.html
7.welcome.html
<%@ page language=java import=java.util.* pageEncoding=ISO-8859-1%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+://+request.getServerName()+:+request.getServerPort()+path+/;
%>
>
welcome!!!
启动tomcat后,在地址栏输入http://localhost:8080/struts2/index 即可打开login.html
输入sa和aaaaaa可打开welcome.jsp