看视频学了一遍,长时间不用,各种忘了。自己整理一遍,以后好复习- -
基本配合,以用户登录为例
一、download下struts开发包,解压apps文件夹下地blank项目。
将该项目文件夹下的开发包添加到web工程中。个人比较喜欢添加到myeclipse下windows->perference->java->Build Path->UserLibrary .以后创建工程方便一些
二、
将实例项目中 struts.xml文件copy到src下,也可以通过myeclipsexml模板,个人喜欢copy,然后修改
例如
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
写jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>
</head>
<body>
<form name="form1" id="f1" action="login" method="post">
<table border="0">
<tr>
<td>Username:</td>
<td><input type="text" name="username" id="username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" id="password"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit"></td>
</tr>
</table>
</form>
</body>
</html>
注意action 或者写全路径<%=path%>+/login 或者不加/ 直接写“login”
写action调用的类文件,注意实现execute()方法,返回"success","input"字段。
实际应用中一般继承ActionSuport父类,然后重写父类execute方法。或者实现Action接口
可以直接调用父类或者接口中得SUCCESS 字段。实际上区别也不大。父类中实现了很多方法,使用起来更加方便,
推荐 继承ActionSupport
package cn.edu.cqupt.action;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
// public class LoginAction implements Action {
public String execute()throws Exception
{
System.out.println(username+"\t"+password);
ActionContext ac= ActionContext.getContext();
Map session = ac.getSession();
if(username.equals("pqz5drf")&&password.equals("zky1110"))
{
System.out.println("runs success");
username="kevin";
session.put("name", username);
return SUCCESS;
}
return INPUT;
}
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;
}
}
编写struts.xml 配置文件
<constant name />标签覆盖org.apache.struts2 default.properties 中的属性,例如struts.action.extension=action 可以更改为<constant name="struts.action.extension" value="do" />
这样仿佛又回到了struts1
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
个人习惯配置成开发模式,这样更改xml后不需要重新加载web工程,项目完成发布后再回false状态。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <include file="example.xml"/> <package name="default" namespace="/" extends="struts-default"> <action name="login" class="cn.edu.cqupt.action.LoginAction"> <result name="success">/welcome.jsp</result> <result name="input">/login.jsp</result> </action> </package> </struts>
web事件流程。
用户提交action: login 。。时间跳转被struts拦截。根据配置未见login事件处理需调用类LoginAction。根据处理结果跳转到欢迎页面(success)或者登录页面"重新登录"