Struts2入门
来源:互联网 发布:淘宝卖家等级分去哪看 编辑:程序博客网 时间:2024/06/08 03:41
今天开始学习Struts2,至于什么是Struts2,Struts2可以用来干嘛,这里就不做介绍了,直接进入主题。
一、搭建环境
1、新建Web项目
2、新建Struts2的配置文件(struts.xml)
在工程src目录下新建一个struts.xml文件,并将Struts2的空项目中的配置文件(struts.xml)的内容复制到新建的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> <!-- devMode是开发者模式,当value="true"时修改这个xml的配置可以不重启服务器而立即生效 --> <constant name="struts.devMode" value="true" /> <!-- package的作用跟Java中包的作用相似,用来解决重名的问题 --> <package name="default" namespace="/" extends="struts-default"> <action name="hello"> <result> /Hello.jsp </result> </action> </package> <!-- Add packages here --></struts>3、导入Struts2相应的jar包及第三方包
将空项目中lib目录中的除junit和spring-test之外的所有文件复制到项目的WebRoot/WEB-INF/lib目录下
4、修改对应的web.xml,建立struts2的filter(参考struts自带的项目),添加如下配置:
<?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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>5、在WebRoot目录下新建并编写Hello.jsp文件
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Hello,Struts2</title></head><body>Hello,Struts2</body></html>6、运行
二、Namespace的作用
Namespace决定了action的访问路径,默认为“”,表示为可以接收所有路径的action,如果没有找到相应的namespace时,则使用namespace为空的action
Namespace可以写为/,或者/xxx,或者/xxx/yyy,对应的action访问路径为/index.action、/xxx/index.action、或者/xxx/yyy/index.action
Namespace最好也用模块来进行命名
<?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> <!-- devMode是开发者模式,当value="true"时修改这个xml的配置可以不重启服务器而立即生效 --> <constant name="struts.devMode" value="true" /> <!-- package的作用跟Java中包的作用相似,用来解决重名的问题 --> <package name="front" extends="struts-default" namespace="/front"> <action name="index"> <!-- 当result的name="success"时,name可以省略 --> <result name="success">/Namespace.jsp</result> </action> </package> <package name="main" extends="struts-default" namespace="/main"> <action name="index"> <result>/Hello.jsp</result> </action> </package> <!-- Add packages here --></struts>
三、Action的用法
具体视图的返回可以由用户自己定义的Action来决定
具体的手段是根据返回的字符串找到对应的配置项,来决定视图的内容
<?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> <constant name="struts.devMode" value="true" /> <package name="front" extends="struts-default" namespace="/"> <!-- class里面是指要调用哪个action类 --> <action name="index" class="com.chongqing.front.IndexAction1"> <!-- result会根据action中execute方法返回的值来确定执行哪个jsp --> <result name="success">/ActionIntroduction.jsp</result> </action> </package></struts>注:<action>标签中的class属性是表示action的对应的类(这个类可以是一个普通Java类),当访问这个action时会创建这个类成为一个对象,然后执行这个对象中的execute方法()(execute方法返回类型为String)。
当<action>标签中class属性省略时,则默认执行com.opensymphony.xwork2.ActionSupport类中的execute方法,而这个方法返回一个字符串常量SUCCESS(常量值为:”success”)。
具体Action实现的方法有三种:
第一种:一个普通Java类中,里面有public String execute方法即可
package com.chongqing.front;public class IndexAction1 {public String execute() {return "success";}}第二种:实现Action接口
package com.chongqing.front;import com.opensymphony.xwork2.Action;public class IndexAction2 implements Action {@Overridepublic String execute() {return "success";}}第三种:从ActionSupport继承
package com.chongqing.front;import com.opensymphony.xwork2.ActionSupport;public class IndexAction3 extends ActionSupport {@Overridepublic String execute() {return "success";}}
一般我们使用的都是第三种方法,好处在于可以直接使用Struts2封装好的方法
四、路径问题
Struts2中的路径问题是因为Struts2的路径是根据action的路径而不是jsp路径来确定的,所以尽量不要使用相对路径。
解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式来拿到webapp的路径)
或者使用myeclipse经常用的,指定basePath
方法如下:
<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><head>//省略....<base href="<%=basePath%>" />//省略....</head>注:<base>标签的作用:当前页面中所有连接都会在前面加上base地址。
- Struts2入门
- struts2 入门
- struts2入门
- struts2-入门
- struts2入门
- struts2入门
- Struts2入门
- struts2入门
- Struts2入门
- struts2 入门
- struts2-入门
- Struts2入门
- struts2 入门
- struts2入门
- Struts2入门
- struts2入门
- Struts2入门
- Struts2入门
- Ucenter通信失败(终极解决)
- 一些很不错的Android开发技巧
- Trump 是一个通过数据库自动发现生成 RESTful 风格接口的框架。
- 使用Tensorflow训练自己的分割数据
- 如何设计一款多场景分布式发号器(Vesta)
- Struts2入门
- 第一次写博客
- Tensorflow之创建我的第一个神经网络
- Redis 高可用方案 Redis Sentinel(后期补)
- Caused by: java.lang.ClassNotFoundException: com.mchange.v2.ser.Indirector
- 剑指offer系列----求double类型的base的exponent次方
- 微信小程序中的input组件
- linux网络编程:多播(组播)编程
- Android--ActionBar