struts2.2.3
Struts2框架的大概处理流程:
1、加载类(FilterDispatcher)
2、读取配置(struts配置文件中的Action)
3、派发请求(客户端发送请求)
4、调用Action(FilterDispatcher从struts配置文件中读取与之相对应的Action )
5、启用拦截器(WebWork拦截器链自动对请求应用通用功能,如验证)
6、处理业务(回调Action的execute()方法)
7、返回响应(通过execute方法将信息返回到FilterDispatcher)
8、查找响应(FilterDispatcher根据配置查找响应的是什么信息如:SUCCESS、ERROER,将跳转到哪个jsp页面)
9、响应用户(jsp--->客户浏览器端显示)
web.xml是系统启动后第一个要找的文件,因为这是struts2的结构,所以第一个得先把这个服务加载起来,文件内容如下,一般没有变化,如果还需要别的服务,才会再在里面加载内容。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
下面是struts.xml文件,这个文件是用来把页面的请求转到具体的操作类,再把操作的结果放回给页面的。是需要按要求配置的。内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//ApacheSoftware Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="login" class="net.mtk.action.LoginAction">
<result name="success">/welcome.jsp</result>
<resultname="input">/login2.jsp</result> 这行当时少了,出错好久
<result name="error">/login2.jsp</result>
</action>
</package>
</struts>
下面是一个登陆界面的login2.jsp界面,内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s"uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login2.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<s:form action="login">
<s:textfield name="username"label="用户名"></s:textfield>
<s:password name="password"label="密码"></s:password>
<s:submit label="登录"></s:submit>
</s:form>
</body>
</html>
LoginAction.java文件,就是具体的操作的文件,内容如下
package net.mtk.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
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;
}
public String execute()throws Exception{
if("hello".equals(this.getUsername().trim())&&"123456".equals(this.getPassword().trim())){
return this.SUCCESS;
}else{
this.addFieldError("username","username or password is error.");
return this.ERROR;
}
}
//重写ActionSupport类中的validate()方法;实现表单验证
@Override
public void validate() {
if(null==this.getUsername()||"".equals(this.getUsername().trim())){
this.addFieldError("username","userName request is null");
}
if(null==this.getPassword() ||"".equals(this.getPassword().trim())){
this.addFieldError("password","password request is null");
}
}
}
welcome.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'welcome.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->
</head>
<body>
欢迎,你已经成功登录
<br>
</body>
</html>
好了,同一色的为对应关系,方面查看所用。至于要不要用这种方法验证,以后再说了。