最近一段时间都在开发网站以及Android app和游戏的后台。在这其中接触到各种框架,为我们的开发带来了不少方便,在这里和各位道友分享一下,希望共同提高。
其实框架是把简单的事情搞的复杂化了,但是方便了开发者,也增加的可移植性。所以我们在使用框架的时候,不要忘了WEB的根本的通信方式,不管是app的C/S,还是web的B/S。都离不开get/post 请求,离不开request/response 对象,变的只是增添了多层马甲而已。假如这个过程都没有弄清楚,连最基本的servlet数据交互过程都不清楚,那么使用框架就有一点得不偿失了。
进入正题,Struts作为MVC 2的Web框架,自推出以来不断受到开发者的追捧,得到用广泛的应用。作为最成功的Web框架,Struts自然拥有众多的优点:
MVC 2模型的使用
功能齐全的标志库(Tag Library)
开放源代码
Struts自身也有不少的缺点:
需要编写的代码过多,容易引起“类爆炸”
单元测试困难
这些缺点随着Web的发展越来越明显。这就促生了Struts 2.0
Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架。
原来我们使用的数据请求Servlet和前台展示页面jsp有极度的耦合性,而struct2恰恰解决了这个问题。如下图
Servlet的高耦合性。
Struts2的MVC结构
一、搭建开发和运行环境
到Apache下载Struts 2包
1.下载struts-2.3.16.1-all.zip 文件
2.打开myEclipse新建Web工程
选择Java EE 7.0
3.在工程右键build path添加struts2的jar包
解压上面下载的zip文件,jar包都在lib文件夹下,需要添加一下Struts的必需jar包
4.打开web.xml添加struts2过滤器,后面内容可以参考官方的example。
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>HelloStruts</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
在2.0以后的版本上有点基础变动,过滤器改为了org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
5.新建首页index.jsp,表单使用struts标签
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <% 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>JSP首页</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> <h3>Say "Hello" to:</h3> <s:form action="hello"> Name: <s:textfield name="name" /> <s:submit /> </s:form> </body> </html>
6.新建action处理类
package com.yk.struts.action; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String execute() { name = "Hello, " + name + "!"; return SUCCESS; } }
7.配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="hello" class="com.yk.struts.action.LoginAction"> <result>/hello.jsp</result> </action> </package> <!-- Add packages here --> </struts>
8.新建跳转界面hello.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% 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>Action响应界面</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> <h3><s:property value="name" /></h3> </body> </html>
8.启动tomcat,搞定。
相关推荐
struts2 配置dtd 自动提示 struts2 配置dtd 自动提示
struts2配置过滤器 示例代码
Struts2配置详解,Struts2配置详解,Struts2配置详解,Struts2配置详解,Struts2配置详解
struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置...
SSH笔记 Struts2配置 demo , 包含随时用的struts2 jar 和简单的配置
struts2配置文件 struts2代码
struts2配置文件加载顺序
·Struts2配置文件介绍 struts.xml action package ·Struts2配置文件介绍 struts.xml action package
struts2 配置SSH 或者简单的JSP文件 里面含有丰富的jar文件
配置SSH框架时关于Struts2的配置文件
struts2配置文件说明,详细说明了怎么配置文件
struts2配置MSSQL连接 <property name="driverClassName"><value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value></property> <property name="url"><value>jdbc:sqlserver://192.168.1.102:1433;...
一个关于struts2配置详解,熟练这个配置可以让你自由配置struts2配置
Struts2配置及使用,你有用的斯蒂芬森的
struts2配置文件传值中文乱码
struts2配置文件介绍struts2配置文件介绍struts2配置文件介绍
struts2配置带中文解释 struts2配置带中文解释struts2配置带中文解释struts2配置带中文解释struts2配置带中文解释struts2配置带中文解释struts2配置带中文解释
Struts2 配置文件手册,Struts2 配置文件手册,Struts2 配置文件手册
NULL 博文链接:https://liubey.iteye.com/blog/703144