ssh第一讲--struts2

Struts2单独使用(Myeclipse版)

一、系统自带

  1. 新建一个普通web project项目

  2. 点击项目,然后点击左上角的project,再点击configure facets,然后点击install apache struts(2.x)facet

二、Maven项目构建

  1. 下载配置好maven组件:myeclipse的maven配置方法

  2. 新建一个maven项目

  3. 右键项目BuildPath–>configure BuildPath–>jre system liabrary移除,再点击右边add liabrary–>jre system liabrary–>workspace default JRE(jdk 1.8xxx),finish就ok了。(已经是jdk1.8就不需要配置这个)

  4. 点击项目,然后点击左上角的project,再点击manage,然后将java版本换成1.8还有Dynamic web module的版本换成3.1

  5. 将web.xml用下面代码覆盖:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>WebDemo</display-name>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      <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>
    
  6. pom.xml加上下面的依赖

            <!-- struts2的依赖 -->
    		<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
    		<dependency>
    			<groupId>org.apache.struts</groupId>
    			<artifactId>struts2-core</artifactId>
    			<version>2.1.8.1</version>
    		</dependency>
          <!-- Servlet api -->
    		<dependency>
    			<groupId>javax.servlet</groupId>
    			<artifactId>javax.servlet-api</artifactId>
    			<version>3.1.0</version>
    			<scope>provided</scope>
    		</dependency>
    
    		<!-- JSTL -->
    		<dependency>
    			<groupId>javax.servlet</groupId>
    			<artifactId>jstl</artifactId>
    			<type>jar</type>
    			<version>1.2</version>
    		</dependency>
    
  7. pom.xml<finalname>下面加上:

    <plugins>
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-compiler-plugin</artifactId>
    				<version>3.6.0</version>
    				<configuration>
    					<source>1.8</source>
    					<target>1.8</target>
    					<encoding>UTF-8</encoding>
    				</configuration>
    			</plugin>
    		</plugins>
    
  8. 将第一系统自带建的项目的resources文件夹下的struts.xml复制一份到maven项目resources文件夹下

    三、struts2的基本用法

<?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="user" extends="struts-default">
		<action name="testAction" class="com.lxf.action.TestAction" method="testMethod" >
			<result name="index" type="dispatcher">
				/index.jsp
			</result>
		</action>
	</package>
</struts>    
  1. package的name可以随便命名,然后extends有两个值: struts-default或json-default

    • struts-default基本数据传输

    • json-default支持基本数据传输和json数据传输,这样看json-default是包括struts-default

    • 当然要实现json数据传输pom.xml还要加上以下依赖:

      <!-- struts使用json的插件 -->
      		<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-json-plugin -->
      		<dependency>
      			<groupId>org.apache.struts</groupId>
      			<artifactId>struts2-json-plugin</artifactId>
      			<version>2.1.8.1</version>
      		</dependency>
      
      		<dependency>
      			<groupId>net.sf.json-lib</groupId>
      			<artifactId>json-lib</artifactId>
      			<version>2.4</version>
      			<classifier>jdk15</classifier>
      		</dependency>
      
      		<dependency>
      			<groupId>commons-lang</groupId>
      			<artifactId>commons-lang</artifactId>
      			<version>2.5</version>
      		</dependency>
      
      		<dependency>
      			<groupId>commons-beanutils</groupId>
      			<artifactId>commons-beanutils</artifactId>
      			<version>1.9.2</version>
      		</dependency>
      
      		<dependency>
      			<groupId>commons-collections</groupId>
      			<artifactId>commons-collections</artifactId>
      			<version>3.2.1</version>
      		</dependency>
      
      		<dependency>
      			<groupId>commons-logging</groupId>
      			<artifactId>commons-logging</artifactId>
      			<version>1.2</version>
      		</dependency>
      		<!-- struts使用json的插件 end -->
      

2.action的name就是前端访问的地址,class是关联的类,method就是默认访问的方法,例如前端访问…/testAction,就是访问下面的类中的testMethod方法

package com.lxf.action;

public class TestAction {
	public String testMethod() {
		System.out.println("测试方法。。。。");
		return "index";
	}
    public String testMethod2() {
		System.out.println("测试方法2。。。。");
		return "index2";
	}
}

如果要访问testMethod2方法访问路径就要这样写:…/testAction!testMethod2,注意:struts2.5以上为了安全默认不能用!访问,此时在action标签中加上strict-method-invocation="false"就行

3.返回值:每个方法执行完就有返回值,action标签下的result标签就是识别返回值并执行操作,testMethod方法执行完返回index字符串,result的name就是对应返回值的,然后type选择返回方式:dispatcher(转发)、redirect(重定向)、json(转发json数据)。result中间的值就是要转发的页面的

OK,struts2的基础知识就分享到这了,感谢观看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值