【SSH (三)】struts2项目搭建

16 篇文章 0 订阅
7 篇文章 0 订阅

1,下载struts2 jar包。

http://struts.apache.org/。

2,Eclipse新建web dynamic project,项目默认是没有web.xml的,通常这时会去网上找,但是又会担心是否靠谱,每一次建项目都得复制粘贴。其实可以让Eclipse生成一份web.xml文件,这个更加方便也更靠谱。方法如下图所示:


填完项目名不要finish,而是选择next下去,然后勾选generate web.xml文件即可。

3,配置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>Struts2Project</display-name>
  
    <!--     配置struts2为项目的controller   -->
    <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>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>
只是加一个核心filter的配置。url-pattern推荐写/*,让struts2来处理所有的请求。

4,添加jar包:

这里我们通常会只想添加最少的jar包,而且要让jar包之前的版本匹配,保证版本一致,最好的办法就是有一个官方的模版。这时我们可以在下载struts2的官网上面下载一个Example Application,如下图:

解压缩之后,里面有一个struts2-blank.war项目,解压,然后就可以得到一个官方的空的struts2项目。我们搭建项目可以参照这个项目。

比如jar的最小集可以从blank里面的lib复制过来。

5,struts.xml:

默认位置在src下面。

同样可以从blank项目中复制过来。修改成我们所需要的即可。

这里我们先配置一个index的action,用于返回一个首页。

<?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.action.extension" value="do,action"/>
    
    <package name="index" extends="struts-default">
        <action name="index" class="com.action.LoginAction" method="index">
            <result name="success">/WEB-INF/jsp/index.jsp</result>
        </action>
    </package>

    <!--     <include file="example.xml"/> -->

</struts>

6,LoginAction类:

package com.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	//返回index界面
	public String index(){
		return SUCCESS;
	}
	
}
继承ActionSupport类,我们可以使用一些struts2提供的功能,比如验证之类的。还可以使用一些表示返回值的常量,这些常量其实是在Action接口中定义的,ActionSupport类本身实现了这个接口。

7,index.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>Index</title>
</head>
<body>
   <h3>index.jsp</h3>
</body>
</html>


8,运行项目,浏览器如下:



搭建成功。


总结一下。主要是配置文件和jar包吧。

1,jar包如果只是小项目搭建,完全可以下载example application,复制即可。如果要用到其他struts2的jar,那么还是得 下载全部的jar包,即-all.zip的那个。然后需要哪个,就从里面复制,不要在其他地方下载,可能会有版本冲突。

2,配置文件,struts.xml和web.xml也都可以找到比较官方的模版,不用担心。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值