IDEA+MAVEN+Struts2项目搭建
1、新建maven web项目
点击“File--New--Project--Maven”,勾选右边的“create from archetype“,根据模版来创建项目,注意在选择模版的时候一定要选择webapp项目,即org.apache.maven.archetypes:maven-archetype-webapp,点击next后,在这里输入groupid:li.abc和artifactid:abc(根据情况而输入不同的值)后点击next,Maven home directory:D:\apache-maven-3.5.3(根据情况而输入不同的值),User setting file:D:\apache-maven-3.5.3\conf\settings.xml,并勾选Override,Local repository:d:\apache-maven-3.5.3\store\(store是手动新建的资料夹),勾选 Override,Porperties选择“archetypeVersion RELEASE”,点击next,Project name:MyMaven2018(或是其它名称),Project location: D:\code\webRoot\MyMaven0526(选择存放的路径,根据情况而修改),最后点击finish
2、选择Project根目录,按F4(或按右键选择Open Module Settings),依次点击“Projecct Settings--Facets--Web”,选择右边的“Deployment Descriptors”,选择“Edit”,更改为*\WEB-INF\web.xml,
3、打开pom.xml添加以下内容
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.2.1</version>
</dependency>
4、在项目根目录下按右键选择“New--Directory”,输入WEB-INF,选择WEB-INF下按右键选择新增“New--File”,输入web.xml(即*\WEB-INF\web.xml),配置内容如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>1401</display-name>
<welcome-file-list>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- Struts2过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<!--如果org.apache.struts是2.5版本,请使用以下class-->
<!--<filter-class>-->
<!--org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter-->
<!--</filter-class>-->
<!--如果org.apache.struts是2.2.1版本,请使用以下class-->
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<!-- Struts2过滤器映射 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
</web-app>
注意:filter-class 的配置, 在 struts-core 低版本比如2.2 配置是不一样的
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
5、在WEB-INF下按右键选择“New--Directory”,输入classes(即*\WEB-INF\classes),选择classes下按右键选择“New--XML Configuration File--Struts Config”,输入struts,struts文件配置如下(即*\WEB-INF\classes\struts.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<!--namespace="/mypack666"后会在网址变成 http://localhost:8080/mavenCh2301Test/mypack666/Login789.action-->
<!--namespace="/" 后网址变成 http://localhost:8080/mavenCh2301Test/Login789.action-->
<!--namespace="/abcd/mypack6668",即网页变成 http://localhost:8080/mavenCh2301Test/abcd/mypack6668/Login789.action,记得同步修改login.htm中的action="abcd/mypack6668/Login789.action"-->
<package name="mypack888" namespace="/abcd/mypack6668" extends="struts-default">
<!--下面的action name="Login789"对应的是login.htm中的action="mypack/Login789.action",都是大小写完全相同的Login789-->
<!--"cn.mypack.Loginn"对应的路径是*\src\main\java\cn\mypack\Loginn.java-->
<action name="Login789" class="cn.mypack.Loginn">
<result>/hello.jsp</result>
</action>
</package>
</struts>
6、在项目录下新建login.htm文件(即:*\login.htm),代码如下
<html>
<head>
<title>hello app</title>
</head>
<body>
<!--*.action:下面的Login789.action对应的是struts.xml中的action name="Login789",是大小写全完相同的Login789,如果更改名称,请同步更改-->
<!--action=?:下面的action="abcd/mypack6668/Login789.action"和struts.xml中的namespace="/abcd/mypack6668",除了前面的/外,其它是完全相同,如果相更改路径名,请同步更改-->
<!--action="mypack6668/Login789.action",即网页变成 http://localhost:8080/mavenCh2301Test/mypack6668/Login789.action,记得同步修改struts.xml-->
<!--<form name="loginForm" method="POST" action="mypack6668/Login789.action">-->
<!--action="abcd/mypack6668/Login789.action",即网页变成 http://localhost:8080/mavenCh2301Test/abcd/mypack6668/Login789.action,记得同步修改struts.xml-->
<form name="loginForm" method="POST" action="abcd/mypack6668/Login789.action">
<table>
<tr>
<td>
<div align="right">User Name:</div>
</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>
<div align="right">Password:</div>
</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit"></td>
<td><input type="reset" name="reset" value="reset"></td>
</tr>
</table>
</form>
</body>
</html>
7、在项目录下新建hello.jsp(即:*\hello.jsp)
<%@page contentType="text/html; charset=utf-8" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title><s:text name="hello.title"/></title>
</head>
<body>
<b>
<s:text name="hello.hello"/>
<s:property value="username"/>
</b>
</body>
</html>
8、新建Loginn.java(路径 *\src\main\java\cn\mypack\Loginn.java)
package cn.mypack;
import com.opensymphony.xwork2.ActionSupport;
public final class Loginn extends ActionSupport {
public String execute() throws Exception {
if (username == null || username.length() == 0)
username = "Guest";
authenticate(username, password);
return SUCCESS;
}
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void authenticate(String username, String password) throws Exception {
//实现对客户验证的业务逻辑,在实际应用中,需要检索数据库来实现此功能。
//本例中直接返回。
return;
}
}
1、新建maven web项目
点击“File--New--Project--Maven”,勾选右边的“create from archetype“,根据模版来创建项目,注意在选择模版的时候一定要选择webapp项目,即org.apache.maven.archetypes:maven-archetype-webapp,点击next后,在这里输入groupid:li.abc和artifactid:abc(根据情况而输入不同的值)后点击next,Maven home directory:D:\apache-maven-3.5.3(根据情况而输入不同的值),User setting file:D:\apache-maven-3.5.3\conf\settings.xml,并勾选Override,Local repository:d:\apache-maven-3.5.3\store\(store是手动新建的资料夹),勾选 Override,Porperties选择“archetypeVersion RELEASE”,点击next,Project name:MyMaven2018(或是其它名称),Project location: D:\code\webRoot\MyMaven0526(选择存放的路径,根据情况而修改),最后点击finish
2、选择Project根目录,按F4(或按右键选择Open Module Settings),依次点击“Projecct Settings--Facets--Web”,选择右边的“Deployment Descriptors”,选择“Edit”,更改为*\WEB-INF\web.xml,
3、打开pom.xml添加以下内容
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.2.1</version>
</dependency>
4、在项目根目录下按右键选择“New--Directory”,输入WEB-INF,选择WEB-INF下按右键选择新增“New--File”,输入web.xml(即*\WEB-INF\web.xml),配置内容如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>1401</display-name>
<welcome-file-list>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- Struts2过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<!--如果org.apache.struts是2.5版本,请使用以下class-->
<!--<filter-class>-->
<!--org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter-->
<!--</filter-class>-->
<!--如果org.apache.struts是2.2.1版本,请使用以下class-->
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<!-- Struts2过滤器映射 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
</web-app>
注意:filter-class 的配置, 在 struts-core 低版本比如2.2 配置是不一样的
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
5、在WEB-INF下按右键选择“New--Directory”,输入classes(即*\WEB-INF\classes),选择classes下按右键选择“New--XML Configuration File--Struts Config”,输入struts,struts文件配置如下(即*\WEB-INF\classes\struts.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<!--namespace="/mypack666"后会在网址变成 http://localhost:8080/mavenCh2301Test/mypack666/Login789.action-->
<!--namespace="/" 后网址变成 http://localhost:8080/mavenCh2301Test/Login789.action-->
<!--namespace="/abcd/mypack6668",即网页变成 http://localhost:8080/mavenCh2301Test/abcd/mypack6668/Login789.action,记得同步修改login.htm中的action="abcd/mypack6668/Login789.action"-->
<package name="mypack888" namespace="/abcd/mypack6668" extends="struts-default">
<!--下面的action name="Login789"对应的是login.htm中的action="mypack/Login789.action",都是大小写完全相同的Login789-->
<!--"cn.mypack.Loginn"对应的路径是*\src\main\java\cn\mypack\Loginn.java-->
<action name="Login789" class="cn.mypack.Loginn">
<result>/hello.jsp</result>
</action>
</package>
</struts>
6、在项目录下新建login.htm文件(即:*\login.htm),代码如下
<html>
<head>
<title>hello app</title>
</head>
<body>
<!--*.action:下面的Login789.action对应的是struts.xml中的action name="Login789",是大小写全完相同的Login789,如果更改名称,请同步更改-->
<!--action=?:下面的action="abcd/mypack6668/Login789.action"和struts.xml中的namespace="/abcd/mypack6668",除了前面的/外,其它是完全相同,如果相更改路径名,请同步更改-->
<!--action="mypack6668/Login789.action",即网页变成 http://localhost:8080/mavenCh2301Test/mypack6668/Login789.action,记得同步修改struts.xml-->
<!--<form name="loginForm" method="POST" action="mypack6668/Login789.action">-->
<!--action="abcd/mypack6668/Login789.action",即网页变成 http://localhost:8080/mavenCh2301Test/abcd/mypack6668/Login789.action,记得同步修改struts.xml-->
<form name="loginForm" method="POST" action="abcd/mypack6668/Login789.action">
<table>
<tr>
<td>
<div align="right">User Name:</div>
</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>
<div align="right">Password:</div>
</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit"></td>
<td><input type="reset" name="reset" value="reset"></td>
</tr>
</table>
</form>
</body>
</html>
7、在项目录下新建hello.jsp(即:*\hello.jsp)
<%@page contentType="text/html; charset=utf-8" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title><s:text name="hello.title"/></title>
</head>
<body>
<b>
<s:text name="hello.hello"/>
<s:property value="username"/>
</b>
</body>
</html>
8、新建Loginn.java(路径 *\src\main\java\cn\mypack\Loginn.java)
package cn.mypack;
import com.opensymphony.xwork2.ActionSupport;
public final class Loginn extends ActionSupport {
public String execute() throws Exception {
if (username == null || username.length() == 0)
username = "Guest";
authenticate(username, password);
return SUCCESS;
}
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void authenticate(String username, String password) throws Exception {
//实现对客户验证的业务逻辑,在实际应用中,需要检索数据库来实现此功能。
//本例中直接返回。
return;
}
}