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

Web开发:Struts2 Spring Hibernate整合(一)——Struts2的使用

2015-09-09 16:59 411 查看

    为了方便包的管理,使用web工程模板方式创建一个Maven工程(这里使用的开发工具为IDEA)

1、搭建web开发测试环境,使用jetty作为服务器(也可以使用Tomcat),只需要在maven的pom.xml中的<build></build>标签中加入以下内容,接着import maven changes,然后build工程,并采用jetty:start方式启动服务器,这时就能访问index.jsp首页

<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.6.10.v20130312</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<!--
<webApp>
<contextPath>/test</contextPath>
</webApp>
-->
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>9090</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>

<stopPort>9091</stopPort>
<stopKey>foo</stopKey>

</configuration>
</plugin>
</plugins>

 2、struts2的使用

    (1)jar包下载:struts2主要用于MVC分离,表单(form action=“”)提交时控制器会将请求转交给对应的Action(extends ActionSupport),为了使用struts2,需要导入对应的jar包,在maven的配置文件pom.xml中加入以下内容:

<!-- struts2依赖包 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.14</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>

     (2)使用struts2:这时可以使用struts2了,为了使用struts2,需要配置web工程中的web.xml文件,告诉工程要使用框架,在web.xml中加入以下内容:

<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>

     (3)struts2配置文件:该配置文件主要配置action到class的map映射,这样在前端只需要填写action名称,struts2就知道交给哪个类来处理,首先创建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 name="struts.devMode" value="false"/>
<!-- 添加struts2-spring整合的插件 -->
<!--<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />-->
<constant name="struts.objectFactory" value="spring" />

<package name="front" namespace="/" extends="struts-default">

<action name="login" class="com.mz.action.LoginAction" method="login">
<result name="success">/index.jsp</result>
<result name="login">/login.jsp</result>
</action>

</package>

</struts>

     (4)前端使用action:首先创建前端页面login.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>登录界面</title>
</head>

<body>
<form action="login" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" /> </td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="password" /> </td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="登录" />
<input type="reset" value="重置" />
</td>
</tr>
</table>
</form>
</body>
</html>

     (5)定义action类:这里的form表单submit时将发出post请求,在服务器端交给login这个action来处理,根据strut.xml配置文件该action对应LoginAction,所以我们创建该类:类定义如下:

package com.mz.action;

import com.mz.service.ILoginService;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
* Created by hadoop on 15-9-7.
*/
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;

public String execute(){
return SUCCESS;
}

public String login() throws IOException {
try {

HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username + ":" + password);
if("admin".equals(username) && "123456".equals(password)){
response.getWriter().write("login success!");
response.getWriter().flush();
return SUCCESS;
}
else {
response.getWriter().write("login failed!");
response.getWriter().flush();
return "login";
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "login";
}

}
}

 这个时候rebuild工程,restart jetty,然后访问localhost:9090/login.jsp,输入用户名和密码即可

(6)<welcome-file>失效,每次访问需要输入完整路径,否则会报匹配不到action的错误,为了避免这个问题可以在struts配置文件中增加如下内容:

<package name="front" namespace="/" extends="json-default">

<default-action-ref name="login" />
<action name="login">
<result name="success">/login.html</result>
</action>

 即当输入网址localhost:9090时,默认使用login.action处理,跳转到登录页面

相关内容

(1)Web开发:Struts2 Spring Hibernate整合(二)——Spring的使用

(2)Web开发:Struts2 Spring Hibernate整合(三)上——Hibernate的使用

(3)Web开发:Struts2 Spring Hibernate整合(三)下——Hibernate的使用

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: