Java-Web进阶系列(五)--maven构建struts2 web工程
2018-02-25 12:02
555 查看
maven构建struts2 web工程
创建maven工程,引入web.xml 上节已讲 戳这里添加Struts依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>oo544.xyz</groupId> <artifactId>Strut2-Demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- struts2核心依赖包 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5.14.1</version> </dependency> <!-- struts2注解 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>2.5.14.1</version> </dependency> <!-- struts2注解返回json格式数据依赖 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-json-plugin</artifactId> <version>2.5.14.1</version> </dependency> </dependencies> </project>
配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Strut2-Demo</display-name> <filter> <filter-name>Struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.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>
配置Struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="base" extends="json-default,struts-default"></package> </struts>
action代码,基于注解传值,跳转页面,返回json格式数据
package oo544.xyz.action; import java.util.HashMap; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import com.opensymphony.xwork2.ActionSupport; @ParentPackage("base") @Namespace("/A") public class Aaction extends ActionSupport { private static final long serialVersionUID = 1L; @Action(value = "show", results = { @Result(name = "success", location = "/A.jsp") }) public String showPage() { msg = StringUtils.isEmpty(msg) ? "haha" : msg; return SUCCESS; } @Action(value = "json", results = { @Result(name = "getJson", type = "json", params = { "root", "jsonMap" }) }) public String getJson() { jsonMap=new HashMap<>(); jsonMap.put("a", "hahah"); jsonMap.put("b", 123); return "getJson"; } private Map<String, Object> jsonMap; public Map<String, Object> getJsonMap() { return jsonMap; } public void setJsonMap(Map<String, Object> jsonMap) { this.jsonMap = jsonMap; } private String msg; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
启动项目;访问:http://127.0.0.1:8080/Strut2-Demo/A/json 返回json格式数据
http://127.0.0.1:8080/Strut2-Demo/A/show 跳转页面
源码下载: 戳这里下载源码
相关文章推荐
- Eclipse 使用 Maven 构建动态 Web 工程,默认无 java 目录的解决方法
- eclipse用maven构建javaweb工程
- maven构建的模块化的JavaWeb工程
- maven工具构建 javaweb 工程中不支持el表达式
- Opencv实现盲水印技术(二)——使用maven构建Java Web工程
- 使用Maven构建JavaWeb工程并设置Dynamic web module版本为3.0
- 手工构建Struts2 Web项目并用Ant构建工程
- Maven构建Java工程
- DOS命令行中用MAVEN构建 Java 和 Java Web 项目
- 用Maven构建Java Web开发环境(Jetty容器)之一
- Maven笔记(四)– eclipse+tomcat构建web工程
- 项目构建之maven篇:8.maven发布web工程及基于spring mvc,jetty实现的用户管理demo
- 把Java Web工程转换为基于Maven的Web工程
- maven构建java项目、web项目
- eclipse+maven+tomcat构建web工程
- Maven构建Web工程和Tomcat7插件使用
- maven构建java项目和web项目
- Spring3.2.0+Hibernate4.1.9+Activiti5.12.1 MAVEN构建JAVA WEB应用之POM.xml
- Maven构建多模块Java工程
- maven创建java工程及web工程