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环境
示例:
![](https://img-blog.csdn.net/20161230163244851?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvR2Fvd2FoYWhh/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
示例:
4.最后完成页面和action的编写
为了避免其他人员知道其他页面路径后跳过过滤器,直接访问页面,所有一般将其他页面放在WEB-INF/content中(若没有content文件夹,自己创建一个)
index.jsp代码示例:
LoginAction.java代码示例:
welcome.jsp或error.jsp代码示例:
网址: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>
相关文章推荐
- Struts2基本框架的搭建
- Struts2+DAO层实现实例02——搭建DAO基本框架并与Struts2组合
- Spring2.5、Struts2、Ibatis开发框架搭建(二)
- Spring2.5、Struts2、Ibatis开发框架搭建(SSI)
- 用Eclipse来搭建Struts2 + Spring2.5 + Hibernate3.2 框架。
- Spring2.5、Struts2、Ibatis开发框架搭建(一)
- Spring2.5、Struts2、Ibatis开发框架搭建
- 对struts2的基本知识和环境的搭建(配图解)
- Mybatis框架的手动搭建以及Mybatis的常用基本应用
- struts2 框架搭建(基本)和几个报错问题
- ArchieOpenGL基础教程第五课:搭建OpenGL基本框架③添加三维图元显示
- struts+spring+hibernate基本项目框架的搭建(1)
- struts2 搭建框架例子如登陆界面,要求是用最新版struts2。
- 对struts2的基本知识和环境的搭建(配图解)
- struts2 基本搭建 helloworld
- Spring2.5、Struts2、Ibatis开发框架搭建
- Spring2.5、Struts2、Ibatis开发框架搭建(三)
- Spring2.5、Struts2、Ibatis开发框架搭建(三)
- ArchieOpenGL教程第一课:搭建opengl基本框架(SDI)①