搭建第一个struts2框架web程序
2014-12-17 11:08
267 查看
以前都是用struts1,技术在发展时代在进步,很遗憾的,我过时了,巴拉巴拉网上struts2资料,整理出了第一个struts2框架程序,这就是该web程序了,
![](http://img.blog.csdn.net/20141217115031906)
1> 接下来我们要把struts2必须的jar包导进去,(稍后我会分享给大家,struts2必须jar包,和该案例源码)
2>配置过滤器:Struts1的入口点是在web.xml中配置的一个Servlet,而Struts2的入口点是一个过滤器(Filter)。因此,Struts2要在web.xml中配置一个过滤器
:<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3> 接下来我们就要编写action类了:Struts1.x中的action类继承了action,Struts2.x的action类需要
继承
ActionSupport,贴上源码
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String username = "";
private String password = "";
@Override
public String execute() throws Exception {
if (checkLogin()) {
return "success";
} else {
return "error";
}
}
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 boolean checkLogin() {
if ("chen".equals(username) && "123456".equals(password)) {
return true;
} else {
return false;
}
}
// 这个东西是反给jsp页面接收数据用的 success.jsp的<s:property>标签是从request对象中获得了一个对象中得到的name属性,有了它和private String username;
//private String password;也就让我们省略了actionForm类了
public String getName() {
return username;
}
}
4>接下来配struts.xml吧
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="mystruts" namespace="/mystruts"
extends="struts-default">
<action name="login" class="action.LoginAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
5> 最后贴上几个简单的jsp
(1)login.jsp,注意,你写的时候把红色注释的去掉再发布
<%@ page language="java" pageEncoding="GBK"%>
<!-- 这里要注意一下,/struts-tags。这里面包含了所有的Struts2标签 -->
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Login</title>
</head>
<body>
<!--<s:form action="login" method="post" namespace="/mystruts">这个namespace要对应struts.xml的<package name="mystruts" namespace="/mystruts"
extends="struts-default">的namespace ;action="login"这里不要写成action="login.action"-->
<s:form action="login" method="post" namespace="/mystruts">
<s:textfield name="username" label=" USERNAME" />
<s:textfield name="password" label=" PASSWORD" />
<s:submit value="SUBMIT" />
</s:form>
</body>
</html>
(2) success.jsp
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>login success</title>
</head>
<body>
<h1>
<s:property value="name" />
</h1>
</body>
</html>
(3)error.jsp
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>login error</title>
</head>
<body>
<h1>
error
</h1>
</body>
</html>
最后的最后了,部署你的工程,发布启动tomcat输入
http://localhost:8080/struts2/login.jsp
该案例下载地址:
http://download.csdn.net/detail/u010401347/8269055#6874737-tsina-1-18852-3048ae643c93bac2c9a415075e9789bc
1> 接下来我们要把struts2必须的jar包导进去,(稍后我会分享给大家,struts2必须jar包,和该案例源码)
2>配置过滤器:Struts1的入口点是在web.xml中配置的一个Servlet,而Struts2的入口点是一个过滤器(Filter)。因此,Struts2要在web.xml中配置一个过滤器
:<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3> 接下来我们就要编写action类了:Struts1.x中的action类继承了action,Struts2.x的action类需要
继承
ActionSupport,贴上源码
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String username = "";
private String password = "";
@Override
public String execute() throws Exception {
if (checkLogin()) {
return "success";
} else {
return "error";
}
}
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 boolean checkLogin() {
if ("chen".equals(username) && "123456".equals(password)) {
return true;
} else {
return false;
}
}
// 这个东西是反给jsp页面接收数据用的 success.jsp的<s:property>标签是从request对象中获得了一个对象中得到的name属性,有了它和private String username;
//private String password;也就让我们省略了actionForm类了
public String getName() {
return username;
}
}
4>接下来配struts.xml吧
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="mystruts" namespace="/mystruts"
extends="struts-default">
<action name="login" class="action.LoginAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
5> 最后贴上几个简单的jsp
(1)login.jsp,注意,你写的时候把红色注释的去掉再发布
<%@ page language="java" pageEncoding="GBK"%>
<!-- 这里要注意一下,/struts-tags。这里面包含了所有的Struts2标签 -->
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Login</title>
</head>
<body>
<!--<s:form action="login" method="post" namespace="/mystruts">这个namespace要对应struts.xml的<package name="mystruts" namespace="/mystruts"
extends="struts-default">的namespace ;action="login"这里不要写成action="login.action"-->
<s:form action="login" method="post" namespace="/mystruts">
<s:textfield name="username" label=" USERNAME" />
<s:textfield name="password" label=" PASSWORD" />
<s:submit value="SUBMIT" />
</s:form>
</body>
</html>
(2) success.jsp
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>login success</title>
</head>
<body>
<h1>
<s:property value="name" />
</h1>
</body>
</html>
(3)error.jsp
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>login error</title>
</head>
<body>
<h1>
error
</h1>
</body>
</html>
最后的最后了,部署你的工程,发布启动tomcat输入
http://localhost:8080/struts2/login.jsp
该案例下载地址:
http://download.csdn.net/detail/u010401347/8269055#6874737-tsina-1-18852-3048ae643c93bac2c9a415075e9789bc
相关文章推荐
- SSH:Struts2框架(搭建运行第一个程序)
- 搭建java web开发环境、使用eclipse编写第一个java web程序
- [置顶] Pycharm+Django搭建第一个Python Web程序
- JSF---->环境搭建及第一个程序
- Spring flex 搭建服务 之 编程环境配置和我的第一个程序(第二章,第一节)
- 如何在Linux下部署web程序(搭建vsftp服务,搭建sshd 服务器,jdk安装步骤,Tomcat安装步骤,MySQL安装步骤)
- Windows Azure入门教学系列 (一): 创建第一个WebRole程序
- 用Eclipse+MyEclipse+WebLogic8.1开发第一个Web程序
- Perl 环境搭建,第一个Perl程序。
- QT window下环境的搭建与编译第一个程序
- Windows Azure入门教学系列 (一): 创建第一个WebRole程序,互联网营销
- 【转】django 搭建的第一个小程序
- 用Eclipse MyEclipse WebLogic8.1开发第一个Web程序
- 全中文演示,从Windows Phone 7开发平台搭建,到建立第一个WP7程序
- 正在学习 android 开发,开发环境已搭建完毕,第一个测试程序已在模拟器上运行成功。
- Spring flex 搭建服务 之 编程环境配置和我的第一个程序(第二章,第二节)
- 我做的第一个web安装部署项目---全国计算机等级考试网上报名缴费程序
- Webcast.NET Remoting学习笔记(1)第一个remoting程序
- 搭建 gxt-2.2.0 环境,让我们第一个简单程序跑起来
- arcgis api for silverlight开发系列之一:平台搭建及第一个应该用程序