JBOSS7下部署服务(二):集成零配置struts2

1、建立两个测试页面

/web/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>index</title>

</head>
<body>
<div id="grid">Hello! Struts2 on JBoss7!</div>
<form action="view/login"  method="post" >
<input type="text" name="name"/>
<input type="password" name="passwd"/>
<input type="submit"/>
</form>
</body>
</html>
/web/WEB-INF/view/main.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>main</title>

</head>
<body>

<div id="grid"><%=request.getAttribute("msg") %></div>

</body>
</html>

2、建立action类

package com.ejb.hello.action;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;

@Namespace(value = "/view")
public class LoginAction extends ActionSupport{
	private Logger logger = Logger.getLogger(LoginAction.class);
	@Action(value = "login", results = { 
			@Result(name = "success", location = "main.jsp"),
			@Result(name = "fail", location = "/index.jsp")})
	public String loginAction() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();

		try {
			System.out.println("name:"+request.getParameter("name"));
			System.out.println("passwd:"+request.getParameter("passwd"));
			
			//throw new Exception("404 错误");
		} catch (Exception e) {
			logger.error(e);
			addActionError(e.getMessage());
			return "fail";
		}
		request.setAttribute( "msg", "Welcome to bj!");
		return "success";
	}

}

3、配置web.xml

/web/WEB-INF/web.xml,增加如下内容
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
		<init-param>
			<param-name>actionPackages</param-name>
			<param-value>com.ejb.hello.action</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/view/*</url-pattern>
	</filter-mapping>
	
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

4、配置/src/struts.xml

此处value值可以根据自己实际情况配置
<constant name="struts.convention.result.path" value="/WEB-INF" />

5、与jboss集成

配置/web/WEB-INF/jboss-deployment-structure.xml
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.1">
  <deployment>
    <dependencies>
      <module name="org.javassist"/> 
      <module name="asm.asm"/>
    </dependencies>
  </deployment>
</jboss-deployment-structure>

6、引用jar

/web/WEB-INF/lib放置struts2要用到的jar

7、生成war

把以上打包生成war,部署到jboss

8、测试


点“提交”后出现“ Welcome to bj!

9、部署完成


你可能感兴趣的:(struts2,jboss)