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

Maven构建Struts2实例

2017-12-28 15:56 309 查看
怎么构建Maven项目就不多说了。很简单,就直接贴代码了。IntelliJ IDEA 创建Maven 工程

项目目录:



pom.xml

可以用Maven的Tomcat7插件,也可以用自己本地的Tomcat。本地的Tomcat在Run>>Edit Configurations中配置。

<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.mistra.ms</groupId>
<artifactId>MistraRMStruts2</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>MistraRMStruts2 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.2</version>
</dependency>

</dependencies>
<build>
<finalName>MistraRMStruts2</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/${project.build.finalName}</path>
<port>8080</port>
<uriEncoding>UTF-8</uriEncoding>
<finalName>${project.build.finalName}</finalName>
<!-- 名称必须与配置文件的id名称一致 -->
<server>tomcat7</server>
</configuration>
<!-- 在打包周期运行tomcat7 -->
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>


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>
<constant name="struts.devMode" value="true"></constant>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.locale" value="zh_CN"></constant>

<package name="hurricane" extends="struts-default">
<action name="loginAction" class="com.mistra.ms.LoginAction" method="execute">
<result>
/success.jsp
</result>
</action>
</package>
</struts>


web.xml

<web-app >
<display-name>Archetype Created Web Application</display-name>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>


LoginAction.java

package com.mistra.ms;

import com.opensymphony.xwork2.ActionSupport;

/**
* Created by Administrator on 2017/12/28/028.
*/
public class LoginAction extends ActionSupport{
public String Name;

public String getName() {
return Name;
}

public void setName(String name) {
Name = name;
}

@Override
public String execute() throws Exception {
return SUCCESS;
}

}


index.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page pageEncoding="UTF-8"%>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<h2>Hello World!</h2>

<form action="loginAction">
姓名<input type='text' name="Name"><input type="submit" value="提交">
</form>
</body>
</html>


success.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page 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>Welcome</title>
</head>
<body>
Hello <s:property value="Name" />

</body>
</html>


源码GitHub地址

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