struts2 web应用框架的简单搭建步骤

    xiaoxiao2021-03-25  151

    Struts2

    Struts2的工作流程:

    在Struts2视图层,获取数据,交给action处理,再返回到视图层。

    一、什么是Strues2

    Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。

    二、搭建Struts2框架的步骤

    开发工具:Myeclipse 8.6.1

    运行环境:Tomcat 6

    项目工程:web工程

        1.创建一个web项目

        2.导入struts2的jar包

        3.定义web.xml中的控制器

    <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <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>     4.定义struts.xml配置文件

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!--package:包 name:包的名字 namespace:命名空间 extends:继承struts-default, --> <package name="users" namespace="/" extends="struts-default"> <!--action:功能,作为控制器 class:类, method:方法 --> <action name="login" class="cn.icloudit.web.action.UserAction" method="login"> <!-- result:结果 --> <result name="login"> /index.jsp </result> <result name="success"> /WEB-INF/content/success.jsp </result> </action> </package> </struts>     5.定义UsersAction

    package cn.icloudit.web.action; public class UserAction { private String uname; private String upwd; public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpwd() { return upwd; } public void setUpwd(String upwd) { this.upwd = upwd; } /** * 登录 * @return */ public String login(){ //如果用户名为“tom”,密码为“123”,返回success if("tom".equals(uname)&&"123".equals(upwd)){ return "success"; } //否则返回login,重新登录 return "login"; } }     6.定义登录页面

    /struts2_test1/WebRoot/index.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <div> <form action="login" method="post"> 用户名:<input type="text" name="uname"><br/> 密 码:<input type="password" name="upwd"><br/> <input type="submit" value="登录"> </form> </div> </body> </html> 7.返回页面

    /struts2_test1/WebRoot/WEB-INF/content/success.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> ${uname},欢迎登录! </body> </html>

    三、启动Tomcat运行测试

    用户名为“tom”,密码为“123”时,登录成功:

    否则回到登录页面:

    四、完整描述整个登录流程,Struts2的工作流程          在index.jsp页面创建一个form表单,其action的值为web项目下struts.xml配置文件action的名称。     在创建一个用户的文本框uname,一个密码框upwd,一个提交按钮submit。     创建一个UsersAction类,其中有uname,upwd属性,写一个login()构造方法。login方法中写入登录的返回值。     在struts.xml文件中设置返回的结果页面,result为success时进入main.jsp页面,result为login时进入index.jsp页面     配置Tomcat,运行测试结果

    四、总结

    Struts2的工作流程     在Struts2视图层,获取数据,交给action处理,再返回到视图层。

    转载请注明原文地址: https://ju.6miu.com/read-3527.html

    最新回复(0)