/*
大家如果喜欢学习struts2可以随时来看看,欢迎大家的到来!
由于我的时间有限,很多地方都不能详细的讲解,因此需要大家的理解和支持,其实学这个,我也只能做一下指导,如果大家有不懂的可以留言询问。大家互相学习学习!
我会在每一天出一篇struts2讲解。如果有事耽搁了,请大家原谅!
*/
/*
大家注意代码中标有红记的地方,是学习的重点。
*/
第一步(view编写):(login.jsp)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!-- 导入struts2的主要标签库,在struts2中去掉了很多不必要的标签,也将标签全部整合在了一起,更方便开发-->
<%
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 'login.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>
<s:form action="login">
<!--注意:login不是与action直接对应,而是通过struts.xml映射文件中的
name=“login”对应-->
<s:textfield name="username" label="用户名"></s:textfield>
<s:password name="password" label="密码"></s:password>
<s:submit label="提交"></s:submit>
<!--在struts2中,表单提交后自动被传给action,其中的set方法会将值暂时保存在内存中,以供使用-->
</s:form>
/*
利用标签创建提交表单,相信大家看得懂。
*/
</body>
</html>
/*
*/
(result.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 'result.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>
${requestScope.username}
<br>
${requestScope.password }
</body>
<!--jsp中的条件表达式,获取forward传递的值,在struts2中,如没进行具体设置,默认情况是服务器端跳转,也就是所说的forword,而并不是请求跳转Redirect-->
</html>
第二步(action)
package com.newstar.struts2.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 9038781564022152594L;
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(){
if("hello".equals(username.trim())&&"world".equals(password.trim())){
return "success";
}else{
this.addFieldError("username","username or password is not right!");
return "failer";
}
/*
execute()只做调控作用,业务逻辑不应该在这里完成,这里因为比较简单,我才写在了这里。业务逻辑交给业务层,我以MVC模式举例。
*/
}
@Override
public void validate() {
if(username==null||"".equals(getUsername().trim())){
this.addFieldError("username", "username is null");
}
if(password==null||"".equals(getPassword().trim())){
this.addFieldError("password", "password is null");
}
}
/*
这个方法是做表单验证的,是struts2中很重要的一部分,后面我会作具体介绍。相信这点代码还是难不倒大家的。
*/
}
第三步(web.xml)
<?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">
<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>
<!--这是struts2的精华部分,大家看好了,大家记得struts1中是怎样配置的吧,这个是不是简单的多了呢。这个就是对所有用户输入数据的过滤器映射,也就是说只要用户有提交数据(不管是空值还是其他确切的值)都会首先经过struts2中的过滤器过滤,然后才传下去。就这么简单,大家感觉到乐趣了吧。。呵呵。。come on everyone!-->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
最后一步(struts.xml)
<?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>
<package name="struts" extends="struts-default">
<action name="login" class="com.newstar.struts2.action.LoginAction">
<result name="input">/login.jsp</result>
<result name="success">/result.jsp</result>
<result name="failer">/login.jsp</result>
</action>
</package>
<!--这里就有点难度了,不过不用怕,后期我会向大家讲解。现在只需简单的了解就足够了package是继承于的web work,其实它的够着方式也是蛮有趣的。action内面的配置其实很简单,相信大家根据英文都能看懂了,就不说了-->
</struts>
好了,今天的就到这了,GOOD BYE!