struts2开发实例

本次开发的struts2实例是部署在jboss5.1.0中,先介绍下 jboss的基本配置

1.jdk的基本配置肯定要有,这里就不说了

2.新建环境变量JBOSS_HOME,值为jboss所在的目录

3.编辑PATH环境变量,添加%JBOSS_HOME%\bin;

4.JBOSS加载的默认配置是jboss-5.1.0.GA\server\default\deploy\jbossweb.sar下的server.xml,所以要修改端口要在该文件下修改,我将端口修改为了8090





再介绍下myeclipse配置jboss,这里用的是myeclipse6.5,直接上图


 配置JDK

 

下面开始用struts2进行开发


struts2应用的开发步骤

 *引入jar包:

commons-fileupload-1.3.1.jar

commons-io-2.2.jar

commons-lang3-3.1.jar

commons-logging-1.1.3.jar

freemarker-2.3.19.jar

扫描二维码关注公众号,回复: 604955 查看本文章

javassist-3.11.0.GA.jar

log4j-api-2.0.jar

log4j-core-2.0.jar

ognl-3.0.6.jar

struts2-core-2.3.16.3.jar

xwork-core-2.3.16.3.jar

  * 在web.xml文件中的核心filter来拦截用户请求

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" 

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_2_5.xsd">

<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.jsp</welcome-file>

  </welcome-file-list>

</web-app>

  * 定义处理用户请求的action类

package com.jiangxl.ssh.web;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

private static final Log log = LogFactory.getLog(LoginAction.class);

@Override

public String execute() throws Exception {

HttpServletRequest request = ServletActionContext.getRequest();

HttpServletResponse response = ServletActionContext.getResponse();

String userName = request.getParameter("userName");

String password = request.getParameter("password");

log.info("userName:"+userName+"\n"+"password:"+password);

  PrintWriter writer = response.getWriter();

  writer.write("1");

return null;

}

}


  * 配置action


从struts2提供的例子中拷贝一个struts.xml放在classpath路径下,配置自己的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.enable.DynamicMethodInvocation" value="false" />

    <constant name="struts.devMode" value="true" />

<constant name="struts.custom.i18n.resources" value="mess"></constant>

<constant name="struts.i18n.encoding" value="GBK"></constant>

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

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

        <global-results>

            <result name="error">/error.jsp</result>

        </global-results>

        <global-exception-mappings>

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

        </global-exception-mappings>

        <action name="index">

            <result type="redirectAction">

                <param name="actionName">HelloWorld</param>

                <param name="namespace">/example</param>

            </result>

        </action>

    </package>

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

      <global-results>

            <result name="error">/error.jsp</result>

        </global-results>

    <action name="login" class="com.jiangxl.ssh.web.LoginAction" method="execute"> <!--配置action-->

    <result name="success">/example/success.jsp</result>  <!--配置成功后的跳转页面,如果前台页面采用ajax方式请求,则个配置则不会起作用,如果通过表单提交,则是有用的-->

    </action>

   

   </package>

    <!-- Add packages here -->

</struts>


  * 编辑视图资源

新建一个login.jsp页面,我引入了jquery来写的页面

<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>

<html>

<head>

<title>登录页面</title>

<script type="text/javascript" src="../lib/jquery-1.11.1.js"></script></head>

<body>

<div style="width:100%">

<fieldset>

<legend>登录信息</legend>

<table>

<tr><td>用户名:</td><td><input type="text" id ="userName"/></td></tr>

<tr><td>密码:</td><td><input type="text" id ="password"/></td></tr>

<tr><td><input type ="button" id = "login" onclick="doLogin()" value="登录"/>

&nbsp;&nbsp;<input type="button" id ="cancel" onclick="cancel()" value="取消"/></td>

</tr>

</table>

</fieldset>

</div>

</body>

</html>

<script type ="text/javascript">

function doLogin(){

var userName =$("#userName").val();

var password = $("#password").val();

var param = {"userName":userName,"password":password};

$.ajax({

url:'example/login.action',  //action的name 是login, namespace="/example"

data:param,

type:'POST',

dataType:'json',

success:function(data,textStatus,jqXHR){

alert(data);

}

});

}

function cancel(){

}

</script>

这样,一个简单的基于struts的web工程就搭建好了

通过myeclipse部署到jboss,在ie下输入对应url就可以访问了。

猜你喜欢

转载自sharis1987.iteye.com/blog/2102624