struts2是由webwork+struts1得来的。很多特性与webwork相似,但与struts1有很大的区别.
我以前没搞过什么webwork(http://baike.baidu.com/view/25660.htm)所以对struts2的机制还很莫生。
运行环境:
servlet contant : apache-tomcat-6.0.20
IDE: myeclipse7.0 (配套的eclipse为3.4)
JDK: jdk1.6.0_03
---------------------------
1. 创建web项目 struts2Test
1.1 在WEB-INF下的lib中拷入struts2的jar包。会在Referenced Libraries中显示。
lib中不会显示,当程序发布后在tomcat中会显示这会jar包.
1.2 项目的目录结构。如下图:
2. 在WebRoot下面建3个jsp文件.
2.1 login.jsp
2.3 failed.jsp
3. 修改WEB-INF下面的web.xml文件
index.jsp
、
这个可以不写
4. 在src下建立action类
在src下建立包com.web 然后建立struts2Action.java类
6.1 在如: D:\tomcat\apache-tomcat-6.0.20\conf 的下面有个server.xml 修改成
docBase为项目所在的位置 reloadable为true时不用返复重启tomcat 一般编写代码时写成true 发布后写成false以保存程序运行的稳定性.
我以前没搞过什么webwork(http://baike.baidu.com/view/25660.htm)所以对struts2的机制还很莫生。
运行环境:
servlet contant : apache-tomcat-6.0.20
IDE: myeclipse7.0 (配套的eclipse为3.4)
JDK: jdk1.6.0_03
---------------------------
1. 创建web项目 struts2Test
1.1 在WEB-INF下的lib中拷入struts2的jar包。会在Referenced Libraries中显示。
lib中不会显示,当程序发布后在tomcat中会显示这会jar包.
1.2 项目的目录结构。如下图:
2. 在WebRoot下面建3个jsp文件.
2.1 login.jsp
Code
<%&#64; page language&#61;"java" import&#61;"java.util.*" pageEncoding&#61;"GBK"%>
<%&#64; taglib prefix&#61;"s" uri&#61;"/struts-tags"%>
<%
String path &#61; request.getContextPath();
String basePath &#61; request.getScheme()&#43;"://"&#43;request.getServerName()&#43;":"&#43;request.getServerPort()&#43;path&#43;"/";
%>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href&#61;"<%&#61;basePath%>">
<title>My JSP &#39;login.jsp&#39; starting pagetitle>
<meta http-equiv&#61;"pragma" content&#61;"no-cache">
<meta http-equiv&#61;"cache-control" content&#61;"no-cache">
<meta http-equiv&#61;"expires" content&#61;"0">
<meta http-equiv&#61;"keywords" content&#61;"keyword1,keyword2,keyword3">
<meta http-equiv&#61;"description" content&#61;"This is my page">
head>
<body>
<s:form name&#61;"pageform" action&#61;"login" namespace&#61;"/" method&#61;"post">
<s:textfield name&#61;"username" label&#61;"用户名" />
<s:password name&#61;"password" label&#61;"密码aa" />
<s:submit value&#61;"登录" />
s:form>
body>
html>
2.2 success.jsp 内容随便去写吧。 <%&#64; page language&#61;"java" import&#61;"java.util.*" pageEncoding&#61;"GBK"%>
<%&#64; taglib prefix&#61;"s" uri&#61;"/struts-tags"%>
<%
String path &#61; request.getContextPath();
String basePath &#61; request.getScheme()&#43;"://"&#43;request.getServerName()&#43;":"&#43;request.getServerPort()&#43;path&#43;"/";
%>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href&#61;"<%&#61;basePath%>">
<title>My JSP &#39;login.jsp&#39; starting pagetitle>
<meta http-equiv&#61;"pragma" content&#61;"no-cache">
<meta http-equiv&#61;"cache-control" content&#61;"no-cache">
<meta http-equiv&#61;"expires" content&#61;"0">
<meta http-equiv&#61;"keywords" content&#61;"keyword1,keyword2,keyword3">
<meta http-equiv&#61;"description" content&#61;"This is my page">
head>
<body>
<s:form name&#61;"pageform" action&#61;"login" namespace&#61;"/" method&#61;"post">
<s:textfield name&#61;"username" label&#61;"用户名" />
<s:password name&#61;"password" label&#61;"密码aa" />
<s:submit value&#61;"登录" />
s:form>
body>
html>
2.3 failed.jsp
3. 修改WEB-INF下面的web.xml文件
Code
xml version&#61;"1.0" encoding&#61;"UTF-8"?>
<web-app version&#61;"2.4"
xmlns&#61;"http://java.sun.com/xml/ns/j2ee"
xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation&#61;"http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcherfilter-class>
filter>
<filter-mapping>
<filter-name>struts2filter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<welcome-file-list>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
web-app>
xml version&#61;"1.0" encoding&#61;"UTF-8"?>
<web-app version&#61;"2.4"
xmlns&#61;"http://java.sun.com/xml/ns/j2ee"
xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation&#61;"http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcherfilter-class>
filter>
<filter-mapping>
<filter-name>struts2filter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<welcome-file-list>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
web-app>
这个可以不写
4. 在src下建立action类
在src下建立包com.web 然后建立struts2Action.java类
Code
package com.web;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class struts2Action extends ActionSupport {
private String username;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password &#61; password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username &#61; username;
}
// &#64;Override
public String execute() throws Exception {
if (this.getUsername().equals("wj")
&& this.getPassword().equals("1234")) {
ActionContext.getContext().getSession().put("user",
this.getUsername());
return "success";
} else {
return "error";
}
}
// &#64;Override
public void validate() {
if (this.getUsername() &#61;&#61; null || this.getUsername().trim().equals("")) {
addFieldError("username", "用户名错误&#xff01;");
}
if (this.getPassword() &#61;&#61; null || this.getPassword().trim().equals("")) {
addFieldError("password", "password 错误&#xff01;");
}
}
}
5. 在src下建立 struts.xml文件package com.web;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class struts2Action extends ActionSupport {
private String username;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password &#61; password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username &#61; username;
}
// &#64;Override
public String execute() throws Exception {
if (this.getUsername().equals("wj")
&& this.getPassword().equals("1234")) {
ActionContext.getContext().getSession().put("user",
this.getUsername());
return "success";
} else {
return "error";
}
}
// &#64;Override
public void validate() {
if (this.getUsername() &#61;&#61; null || this.getUsername().trim().equals("")) {
addFieldError("username", "用户名错误&#xff01;");
}
if (this.getPassword() &#61;&#61; null || this.getPassword().trim().equals("")) {
addFieldError("password", "password 错误&#xff01;");
}
}
}
Code
xml version&#61;"1.0" encoding&#61;"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&#61;"login" namespace&#61;"/" extends&#61;"struts-default">
<action name&#61;"login" class&#61;"com.web.struts2Action">
<result name&#61;"error">failed.jspresult>
<result name&#61;"success">success.jspresult>
<result name&#61;"input">login.jspresult>
action>
package>
struts>
6. 发布运行xml version&#61;"1.0" encoding&#61;"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&#61;"login" namespace&#61;"/" extends&#61;"struts-default">
<action name&#61;"login" class&#61;"com.web.struts2Action">
<result name&#61;"error">failed.jspresult>
<result name&#61;"success">success.jspresult>
<result name&#61;"input">login.jspresult>
action>
package>
struts>
6.1 在如&#xff1a; D:\tomcat\apache-tomcat-6.0.20\conf 的下面有个server.xml 修改成
docBase为项目所在的位置 reloadable为true时不用返复重启tomcat 一般编写代码时写成true 发布后写成false以保存程序运行的稳定性.
<Context path&#61;"/struts2Test" docBase&#61;"D:\project\java_workspace\struts2Test\WebRoot" reloadable&#61;"true"/>
-->
Host>
Engine>
Service>
Server>
6.2 http://localhost:8088/struts2Test/login.jsp 点按钮提交后会变成 http://localhost:8088/struts2Test/login.action-->
Host>
Engine>
Service>
Server>