Strut2环境搭建+入门实例(1)
2016-12-18 18:21
232 查看
Struts版本:2.3.14
工具:Eclipse Tomcat7.0
一、Eclipse中新建名为Struts2的web工程:
二、在WEB-INF下的lib中导入必需的Jar包:
右键Build Path-Add to Build Path
三、配置web.xml(版本不同,对应的org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter可能不一样)
四.jsp页面
1.登录页面:login.jsp
2.登陆成功页面:success.jsp
3.登录失败页面:fail.jsp
五、建立Action
在src下建立如下java文件和struts.xml
打开LoginAction.java编写代码
定义用户名和密码(定义的常量要和jsp页面input的name一致哦)
编写execute()方法
六、struts.xml配置
七、运行Tomcat,查看结果
用户名:123
密码:234
输入正确,跳转正确页面:
输入错误,跳转错误页面
注意事项:
1.尽量到官网下载完整的jar包
2.如果在配置web.xml和struts.xml时出现困难可以按照以下步骤:
下载完整的jar包基本包含如图信息
打开apps–解压struts2-rest-showcase.war文件;
在WEB-INF中有web.xml文件的配置;
在WEB-INF\src\java中有struts.xml的配置;
按照配置就会减少出错啦~~~~
工具:Eclipse Tomcat7.0
一、Eclipse中新建名为Struts2的web工程:
二、在WEB-INF下的lib中导入必需的Jar包:
右键Build Path-Add to Build Path
三、配置web.xml(版本不同,对应的org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter可能不一样)
<?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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>struts2</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.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
四.jsp页面
1.登录页面:login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> < 4000 span class="hljs-tag"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Struts2登录实例</title> </head> <body> <form action="login"> 用户名: <input type="text" name="username" > <br> 密码: <input type="password" name="password"> <br> <input type="submit" value="登录"> </form> </body> </html>
2.登陆成功页面:success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" 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>登录成功界面</title> </head> <body> <h1>恭喜你登录成功!</h1> </body> </html>
3.登录失败页面:fail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" 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>Insert title here</title> </head> <body> 很遗憾,你输错了哦 </body> </html>
五、建立Action
在src下建立如下java文件和struts.xml
打开LoginAction.java编写代码
定义用户名和密码(定义的常量要和jsp页面input的name一致哦)
编写execute()方法
package com.struts.demo; public class LoginAction { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String execute() throws Exception { if (getUsername().equals("123") && getPassword().equals("234")) { return "success";//result对应的返回值 } else { return "error"; } } }
六、struts.xml配置
<?xml version="1.0" encoding="UTF-8"?> <!--struts版本不一致,dtd信息会有一定的差异,应使用与版本相对应的dtd信息 --> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- name随便取,extends="struts-default"为默认值--> <package name="pp" extends="struts-default"> <!-- action的name对应jsp页面上的action,class为包名+类名 result的name对应LoginAction中execute()方法中对应的返回值--> <action name="login" class="com.struts.demo.LoginAction"> <result name="success">/success.jsp</result> <result name="error">/fail.jsp</result> </action> </package> </struts>
七、运行Tomcat,查看结果
用户名:123
密码:234
输入正确,跳转正确页面:
输入错误,跳转错误页面
注意事项:
1.尽量到官网下载完整的jar包
2.如果在配置web.xml和struts.xml时出现困难可以按照以下步骤:
下载完整的jar包基本包含如图信息
打开apps–解压struts2-rest-showcase.war文件;
在WEB-INF中有web.xml文件的配置;
在WEB-INF\src\java中有struts.xml的配置;
按照配置就会减少出错啦~~~~
相关文章推荐
- Activiti环境搭建(非与Spring整合),以及入门实例--001
- SpringMVC入门学习(一) 环境搭建+实例演示
- 【Hibernate】—Hibernate+mysql 环境搭建+入门实例
- SpringMVC入门学习(一) 环境搭建+实例演示
- Spark+ECLIPSE+JAVA+MAVEN windows开发环境搭建及入门实例【附详细代码】
- 新人入门linux环境搭建及shell文件运行测试实例
- RobotFramework+Selenium2环境搭建与入门实例
- RobotFramework+Selenium2环境搭建与入门实例
- SpringMVC入门学习 环境搭建+实例演示
- 3、RobotFramework+Selenium2环境搭建与入门实例
- RobotFramework+Selenium2环境搭建与入门实例
- Spring MVC实例入门之环境搭建
- RobotFramework+Selenium2环境搭建与入门实例
- 【Hibernate】从入门到精通(二)环境搭建实例
- Mybatis入门:(1)环境搭建及第一个实例
- android下NDK开发环境搭建及TestJNI入门实例完整过程
- Spark+ECLIPSE+JAVA+MAVEN windows开发环境搭建及入门实例【附详细代码】
- SpringMVC入门学习(一) 环境搭建+实例演示
- RobotFramework+Selenium2环境搭建与入门实例
- SpringMVC入门学习(一) 环境搭建+实例演示