搭建第一个struts2框架web程序
来源:互联网 发布:条码软件label matrix 编辑:程序博客网 时间:2024/06/05 08:45
以前都是用struts1,技术在发展时代在进步,很遗憾的,我过时了,巴拉巴拉网上struts2资料,整理出了第一个struts2框架程序,这就是该web程序了,
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
- 搭建第一个struts2框架web程序
- SSH:Struts2框架(搭建运行第一个程序)
- 《struts2》框架第一个实例的搭建
- 搭建第一个struts2
- 第一个struts2程序
- 第一个struts2程序
- struts2第一个程序
- struts2 第一个程序
- 第一个struts2程序
- Struts2第一个程序
- 第一个struts2程序
- 第一个Struts2程序
- 第一个struts2程序
- 第一个struts2程序
- 第一个 Struts2 程序
- struts2第一个程序
- struts2第一个程序
- 第一个struts2程序
- 入门经典题
- 黑马程序员--IOS-学习笔记-UI02
- Python中Cookie的处理(二)cookielib库
- AiX--Ipcs 资源
- mac下python与redis结合
- 搭建第一个struts2框架web程序
- HHVM 是如何提升 PHP 性能的?
- Web系统大规模并发——电商秒杀与抢购
- 我在项目中Springmvc异常日志处理(二) ps:如何有更好的方法,请写到评论中多多交流,谢谢!
- 详解Oracle几种性能调优方法
- 回撤换行
- 嵌入式 死锁&活锁含义
- 企业收购并购
- DirectX--yuv420p上实现的字符叠加