• Eclipse搭建struts2框架


    新建动态web项目

    输入项目名称;

     

    New Runtime后选择如下,

     

    选择tomcat的安装目录;

     

    然后如下,完成;

     

    当前的目录结构如下;之前要配置好java环境;

     

    如果创建项目时没有选中生成web.xml,右击项目文件夹,选择如下菜单,生成web.xml;

     

    把struts2的包拷贝到lib文件夹下,然后刷新一下,会显示出来;

     

    选中所有的包,右击,选择如下菜单,把包加入项目;

     

    在src文件夹下创建struts.xml,

     

    struts.xml,

    1. "1.0" encoding="UTF-8"?>
    2. struts PUBLIC
    3. "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    4. "http://struts.apache.org/dtds/struts-2.5.dtd">
    5. <struts>
    6. <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    7. <package name="default" namespace="/" extends="struts-default">
    8. <action name="login" class="com.example.struts2.LoginAction" method="login">
    9. <result name="success">/success.jspresult>
    10. <result name="error">/error.jspresult>
    11. action>
    12. package>
    13. struts>

    右击src文件夹,创建新的包, 

     

    输入包名,

     

    Finish之后如下,

     

    右击新创建的包,创建新的类,

     

    输入类名;

     

    1. package com.example.struts2;
    2. import javax.servlet.http.HttpServletRequest;
    3. import org.apache.struts2.ServletActionContext;
    4. public class LoginAction {
    5. HttpServletRequest req = ServletActionContext.getRequest();
    6. String username = req.getParameter("username");
    7. String password = req.getParameter("password");
    8. public String getUsername() {
    9. return username;
    10. }
    11. public void setUsername(String username) {
    12. this.username = username;
    13. }
    14. public String getPassword() {
    15. return password;
    16. }
    17. public void setPassword(String password) {
    18. this.password = password;
    19. }
    20. public String login(){
    21. if("xiaoBaby".equals(username)
    22. && "123456".equals(password)){
    23. return "result";
    24. }else{
    25. return "error";
    26. }
    27. }
    28. }

    然后再创建3个jsp;

    index.jsp,

    1. <%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
    2. html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    3. <html>
    4. <head>
    5. <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    6. <title>Insert title heretitle>
    7. head>
    8. <body>
    9. <form action="login.action" method="post">
    10. 用户名:<input type="text" name="username">
    11. 密码:<input type="text" name="password">
    12. <input type="submit" value="提交">
    13. form>
    14. body>
    15. html>

    success.jsp和error.jsp简单化,在之间添加一个字符串即可; 

     此时项目结构如下,

     

    还有web.xml代码;

    1. "1.0" encoding="UTF-8"?>
    2. <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" version="3.1">
    3. <display-name>stsdisplay-name>
    4. <filter>
    5. <filter-name>struts2filter-name>
    6. <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilterfilter-class>
    7. filter>
    8. <filter-mapping>
    9. <filter-name>struts2filter-name>
    10. <url-pattern>/*url-pattern>
    11. filter-mapping>
    12. <welcome-file-list>
    13. <welcome-file>index.htmlwelcome-file>
    14. <welcome-file>index.htmwelcome-file>
    15. <welcome-file>index.jspwelcome-file>
    16. <welcome-file>default.htmlwelcome-file>
    17. <welcome-file>default.htmwelcome-file>
    18. <welcome-file>default.jspwelcome-file>
    19. welcome-file-list>
    20. web-app>

    然后右击项目文件夹,选择如下菜单,

     

    出现如下对话框,Finish;

     

    index.jsp不会出来,查看控制台输出,包含一个错误,

        java.lang.ClassNotFoundException: .apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter,

    我想把web.xml中的

    org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter

    改为

       
                org.apache.struts2.dispatcher.FilterDispatcher
       


    然后再重新运行,又出现下图错误;有时间继续;

     

  • 相关阅读:
    14:00面试,14:05就出来了,问的问题有点变态。。。
    【博客547】keepalived实现vip的原理剖析
    【CMU15-445 Part-4】DatabaseStorage ii
    抖音矩阵系统源码独立部署,抖音SEO源码定制。
    知识变现海哥:如何把知识卖的更贵、更多、更酷
    为什么我不推荐去SAP培训机构参加培训?
    【MyCat简单介绍】
    惊喜:2023前瞻版Java面试指南,不止八股文
    Centos7搭建SVN代码控制服务器
    电力物联网全场景安全态势感知解决方案
  • 原文地址:https://blog.csdn.net/bcbobo21cn/article/details/132847114