您的位置:首页 > 编程语言 > Java开发

S2SH整合以及图片上传(5)之struts2的搭建

2018-03-05 16:50 387 查看
    经过前面一番操作之后,我们已经把spring和hibernate整合搭建完毕。
    接下来,我们对最后一个框架struts2进行搭建。

    详细步骤如下。

    第一步,我们依然导入相关的jar包到新建的web项目的/TestUpload/WebRoot/WEB-INF/lib目录中:

    


    第二步,我们在项目的src目录下新建struts2的核心配置文件struts.xml:

    


     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="true"></constant>

<!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>

    <!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
    <constant name="struts.devMode" value="true" />

    <!-- 允许静态方法调用 -->
    <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>

    <!-- 设置文件最大上传的容量大小,这里设置的是10M -->
    <constant name="struts.multipart.maxSize" value="10701096" />

    <!-- Struts2和Spring集成的一个比较关键的配置,与Spring集成时,指定由Spring负责action对象的创建 -->
 <constant name="struts.objectFactory" value="spring" />

</struts>


    第三步,我们需要在项目的web.xml中配置struts:
   ( web.xml的位置:TestUpload/WebRoot/WEB-INF/web.xml)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd"> <display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- 配置struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>struts.i18n.encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>
    第四步,我们先创建需要的jsp页面index.jsp,add.jsp,operate_success.jsp:
    index.jsp内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>Person Page</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>
<h2>S2SH整合以及图片上传</h2>
<a href="${pageContext.request.contextPath}/add.jsp">添加人员</a>
<hr />
</body>
</html>
    add.jsp内容如下:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>Person Operate Page</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>
<h2>S2SH整合以及图片上传</h2>
<h2>添加人员</h2>
<s:form action="person!add" namespace="/person" method="post" enctype="multipart/form-data">
<table>
<tr>
<td><input type="submit" value="上传" /></td>
</tr>
</table>
</s:form>

</body>
</html>
    operate_success.jsp内容如下:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>Operate Success Page</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>
<h2>S2SH整合以及图片上传</h2>
<h2>操作成功</h2>
<a href="${pageContext.request.contextPath}/index.jsp">返回主页</a>
</body>
</html>
    第五步,我们在项目的src目下新建一个包,用来存储action:
    


    PersonAction内容如下:
package com.ssh.action;

import com.opensymphony.xwork2.ActionSupport;

public class PersonAction extends ActionSupport{

private static final long serialVersionUID = 1L;

public String add(){
return SUCCESS;
}
}    第六步,我们配置struts的核心配置文件struts.xml,添加如下内容:
<package name="person" namespace="/person" extends="struts-default" >

<action name="person" class="personAction">
<result name="success">/operate_success.jsp</result>
</action>
</package>    第七步,由于我们是S2SH整合开发,所以struts中的action将交由spring来接管,即在applicationContext.xml中配置如下内容:
    


    代码如下:
<!-- 配置action的bean -->
<bean id="personAction" class="com.ssh.action.PersonAction" scope="prototype" />    第八步,我们需要在web.xml中配置applicationContext.xml的启动:    <!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!-- 对Spring容器进行实例化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
    第九步,测试struts框架是否搭建成功:
    控制台显示成功:



    在网页中显示成功:

    


    


    


    至此,struts框架搭建完毕!

    S2SH整合以及图片上传(6)之图片上传
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ssh整合 图片上传