maven 下struts 框架搭建(系列一)

记忆随着季节轮回,留下几只脚印。


本人开发环境为win7 ,开发工具eclipse;jre1.8.0_31

下载的包大多数都是最新版本~当然只限于2015的最新~

jdk struts2等安装此处省略。

1.maven安装与环境配置,问一下度娘~或点击下面链接:

http://www.cnblogs.com/smile2010/archive/2011/11/22/2259535.html

http://blog.csdn.net/yyywyr/article/details/20304169

2.eclipse maven上配置:

http://jingyan.baidu.com/article/5d368d1e306a913f61c05743.html

3.项目开发

3.1项目创建

首先创建maven project,问度娘~这个有很多吐舌头

创建完后生成目录如下

maven 下struts 框架搭建(系列一)_第1张图片


3.2配置文件

3.2.1  pom.xml的配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>qq</groupId>
  <artifactId>testSSM</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>testSSM Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
  	<spring.version>4.1.5.RELEASE</spring.version>
  	<mybatis.version>3.2.8</mybatis.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- 导入java ee jar 包 -->
	<dependency>
		<groupId>javax</groupId>
		<artifactId>javaee-api</artifactId>
		<version>7.0</version>
	</dependency>
	<!-- 导入struts 包 -->
     <dependency>  
        <groupId>org.apache.struts</groupId>  
        <artifactId>struts2-core</artifactId>  
        <version>2.3.4</version>  
    </dependency>  
    <dependency>  
        <groupId>org.apache.struts</groupId>  
        <artifactId>struts2-json-plugin</artifactId>  
        <version>2.3.4</version>  
    </dependency> 
    <dependency>  
      <groupId>commons-lang</groupId>  
      <artifactId>commons-lang</artifactId>  
      <version>2.6</version>  
      <scope>provided</scope>  
    </dependency>
    <!-- 导入mybatis包 -->
    <dependency>
	<groupId>org.mybatis</groupId>
	<artifactId>mybatis</artifactId>
	<version>3.2.6</version>
     </dependency>
	<!-- mybatis/spring包 -->
	<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.2.2</version>
	</dependency>
	<!-- mybatis核心包 -->
	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis</artifactId>
		<version>${mybatis.version}</version>
	</dependency>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.11</version>
		<scope>test</scope>
	</dependency>
	<!-- spring核心包 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>


		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-oxm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring.version}</version>
		</dependency>


		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>


		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>


		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>


		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>
  </dependencies>
  <build>
    <finalName>testSSM</finalName>
  </build>
</project>

当然上面代码也包含了spring和mybatis的依赖包。maven管理下的项目好像不允许将jar包直接粘贴到lib里,通过配置dependency会把相应的包自动下载下来。如果需要特别配置,以下博客提供了两种方式。

http://liugang594.iteye.com/blog/1677712

pom.xml详解,可以点击以下链接,写的很详细:

http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope

http://blog.csdn.net/longeremmy/article/details/9670619

http://my.oschina.net/zh119893/blog/232896

3.2.2 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="json-default">

        <default-action-ref name="index" />

        <global-results>
            <result name="error">/WEB-INF/jsp/error.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>

        <action name="Hello" class="testStruts.HelloWorld" method="Hello">
            <result type="json"></result>
        </action>
    </package>

    <!-- Add packages here -->

</struts>

这个配置文件是从struts blank里边拷贝过来的,里面添加了一个action,是下面java文件的一个配置。注意package里边继承了json-default;

3.2.3 web.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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">

    <display-name>Struts Blank</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>
这个配置文件是从struts blank里边拷贝过来的,没有特别之处。
3.3 测试程序

3.3.1 java代码

<pre name="code" class="java">package testStruts;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String msg;
	private String json;

	public String Hello(){
		if(msg==null)
		    System.out.println("the msg is null");
		else
			 System.out.println("the msg is"+msg);
		json="just a test";
		return SUCCESS;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

}

 注意struts里边传参,要把属性的setter和getter方法写上,当然这只是方法之一。后话,在加上spring之后,为了安全以及负载性选择性的把一些属性暴露出来,也就是说setter和getter方法选择性的添加。 
 

此类继承了ActionSupport,当然实现方式不止这一种方式。一本书写的不错,推荐一下~《struts2 in action》里面写的原理很详细~

        <action name="Hello" class="testStruts.HelloWorld" method="Hello">
            <result type="json"></result>
        </action>
这是配置文件里的相关配置,回调方法是hello,当然由于回调方法返回了一个常量SUCCESS,是result的name默认值,所以执行成功会返回type为json的数据。

3.3.2 jsp页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="UTF-8"%>
<html>
<body>
<h2>Hello World!</h2>
<form action="Hello" method="get">
	<input name="msg"  type="text">
	<input type="submit" value="Submit" />
</form>

</body>
</html>

action指定将form提交后的数据转给Hello的action,submit之后会以get方式在struts.xml里找name为Hello的action执行。

3.3运行结果:

web返回的结果

{"json":"just a test","msg":"546464"}



你可能感兴趣的:(java,maven,框架,struts,helloworld,ee)