• Struts2入门教程


    最近闲来无事,学习s2sh框架,这里先写一点struts2的入门

    我的环境

    eclipse 4.3.2

    tomcat 7.0.52

    jdk 1.7.0_45

    struts2 2.3.16.3

    在eclipse中配置好tomcat,安装好struts2(将基本jar包拷到WebContent-->WEB-INFO-->lib中)

    在WEB-INFO中配置好web.xml(可以在struts2的apps里面找到,修改一下)注意web.xml的路径

    web.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
     3     <filter>
     4         <filter-name>struts2</filter-name>
     5         <!--  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>-->
     6         <!-- org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter -->
     7         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
     8     </filter>
     9 
    10     <filter-mapping>
    11         <filter-name>struts2</filter-name>
    12         <url-pattern>/*</url-pattern>
    13     </filter-mapping>
    14     <welcome-file-list>
    15         <welcome-file>index.jsp</welcome-file>
    16     </welcome-file-list>
    17 
    18 </web-app>

    在WebContent下面新建一个login.jsp页面

    login.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8" import="java.util.*"
     2     pageEncoding="UTF-8"%>
     3 <%@ taglib uri="/struts-tags" prefix="s" %>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     8 <title>Login Page</title>
     9 </head>
    10 <body>
    11     <s:form action="login" method="post" >
    12         <s:textfield name="username"/>
    13         <s:textfield name="password" type="password"/>
    14         <s:submit value="login"/>
    15     </s:form>
    16 </body>
    17 </html>

    新建一个Action类,这里我建的类路径为com.gxf.struts2.LoginAction

    LoginAction.java

     1 package com.gxf.struts2;
     2 
     3 import com.opensymphony.xwork2.ActionSupport;
     4 
     5 @SuppressWarnings("serial")
     6 public class LoginAction extends ActionSupport {
     7     private String username;
     8     private String password;
     9     @Override
    10     public String execute() throws Exception {
    11         if(username.equals("luckygxf") && password.equals("luckygxf")){
    12             return SUCCESS;
    13         }
    14         else{
    15             return INPUT;
    16         }
    17     }
    18     
    19     public String getUsername() {
    20         return username;
    21     }
    22     public void setUsername(String username) {
    23         this.username = username;
    24     }
    25     public String getPassword() {
    26         return password;
    27     }
    28     public void setPassword(String password) {
    29         this.password = password;
    30     }
    31     
    32     
    33 }

    在src目录下面新建一个struts.xml(这里也可以在下载的struts2中找到,修改一下就可以了)

    struts.xml

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4     "http://struts.apache.org/dtds/struts-2.3.dtd">
     5 
     6 <struts>
     7     <package name="login" namespace="/" extends="struts-default">
     8         <action name="login" class="com.gxf.struts2.LoginAction">
     9             <result name="success">/success.jsp</result>
    10             <result name="input">/login.jsp</result>
    11         </action>
    12 
    13     </package>
    14 
    15 </struts>

    在WebContent下面新建一个success.jsp页面

    success.jsp

     1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
     2     pageEncoding="ISO-8859-1"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     7 <title>success.jsp</title>
     8 </head>
     9 <body>
    10     <h1>success.jsp</h1>
    11 </body>
    12 </html>

    ok,所有的配置已经写好。将项目部署到tomcat中,启动tomcat。查看控制台的输出信息,没有错误。在浏览器中输入http://localhost:8080/Struts2/login.jsp

    用户名和秘密都输入luckygxf。登陆成功,跳转到success.jsp页面。

  • 相关阅读:
    Qt中使用C#的dll
    一、深拷贝和浅拷贝
    1、手撕Promise上
    JavaScript之四大继承方法
    C++ 模板编程简单示例程序
    C++深拷贝浅拷贝示例程序
    C++虚基类 类接口应用示例程序
    C++使用定义静态类,禁止实例化&&继承程序示例(借助纯虚函数是的只能以类名访问属性和方法)
    202204092012_《Clion激活小记》 Coca
    202204091505_《GNU ARM Toolchain安装成功》 Coca
  • 原文地址:https://www.cnblogs.com/luckygxf/p/3736698.html
Copyright © 2020-2023  润新知