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

Struts2基本框架搭建

2016-12-30 16:53 197 查看
1.首先需要登录Apache官网下载和安装Struts2框架

网址:https://struts.apache.org/download.cgi 里边有多个版本,推荐下载:Full Distribution版本。

2.将下载的压缩文件解压,然后再将解压后的Apps中的struts2-blank.war继续解压

然后在路径:apps\struts2-blank\WEB-INF\lib中有13个压缩文件

3.搭载Struts环境

示例:



(2)在src根目录下新建struts.xml文件,然后将:struts-2.3.31-all\struts-2.3.31\apps\struts2-blank\WEB-INF\src\java路径下的struts.xml文件内容粘到新建的Struts.xml中


示例:

<?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>
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="com.iotek.action.LoginAction">
<result name="error">/WEB-INF/content/error.jsp</result>
<result name="success">/WEBINF/content/welcome.jsp</result>
</action>
</package>
</struts>


(3)d)在web.xml中添加struts2的过滤器
示例:


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>StrutsKetang</display-name>
<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>
//以下为添加的过滤器部分
<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>
</web-app>


4.最后完成页面和action的编写

为了避免其他人员知道其他页面路径后跳过过滤器,直接访问页面,所有一般将其他页面放在WEB-INF/content中(若没有content文件夹,自己创建一个)

index.jsp代码示例:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login">//这里的login就是struts.xml中定义的action的name
姓名:<input type="text" name="name"><br>
密码:<input type="password" name="password"><br>
<input type="submit" name="submit" value="提交">
<input type="reset" value="取消">
</form>
</body>
</html>


LoginAction.java代码示例:

package com.iotek.action;

public class LoginAction {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute(){
if(getName().equals("xiaoming")&&getPassword().equals("123456")){
System.out.println("登录成功");
return "success";
}else{
return "error";
}

}

}


welcome.jsp或error.jsp代码示例:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${name }
${password }
<p>登录成功</p>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息