首先在struts2.xml文件配置一个包,在包中配置一个action,新建action,新建视图,在action中定义由method定义的方法,这个方法一定要返回String类型,返回的是视图的名称。

直接上图

一,首先是pom.xml,引入所需jar

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10.  
  11. http://www.apache.org/licenses/LICENSE-2.0
  12.  
  13. Unless required by applicable law or agreed to in writing,
  14. software distributed under the License is distributed on an
  15. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. KIND, either express or implied. See the License for the
  17. specific language governing permissions and limitations
  18. under the License.
  19. -->
  20. <!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ -->
  21. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  22.  
  23. <modelVersion>4.0.0</modelVersion>
  24. <packaging>war</packaging>
  25.  
  26. <name>Strtus</name>
  27. <groupId>com.cyf</groupId>
  28. <artifactId>Strtus</artifactId>
  29. <version>1.0-SNAPSHOT</version>
  30.  
  31. <build>
  32. <plugins>
  33. <plugin>
  34. <groupId>org.mortbay.jetty</groupId>
  35. <artifactId>maven-jetty-plugin</artifactId>
  36. <version>6.1.7</version>
  37. <configuration>
  38. <connectors>
  39. <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
  40. <port>8888</port>
  41. <maxIdleTime>30000</maxIdleTime>
  42. </connector>
  43. </connectors>
  44. <webAppSourceDirectory>${project.build.directory}/${pom.artifactId}-${pom.version}</webAppSourceDirectory>
  45. <contextPath>/</contextPath>
  46. </configuration>
  47. </plugin>
  48. <plugin>
  49. <artifactId>maven-resources-plugin</artifactId>
  50. <version>2.6</version>
  51. <executions>
  52. <execution>
  53. <id>copy-xmls</id>
  54. <phase>process-resources</phase>
  55. <goals>
  56. <goal>copy-resources</goal>
  57. </goals>
  58. <configuration>
  59. <outputDirectory>${basedir}/target/classes</outputDirectory>
  60. <resources>
  61. <resource>
  62. <directory>${basedir}/src/main/java</directory>
  63. <includes>
  64. <include>**/*.xml</include>
  65. </includes>
  66. </resource>
  67. </resources>
  68. </configuration>
  69. </execution>
  70. </executions>
  71. </plugin>
  72. </plugins>
  73. </build>
  74. <!-- 属性配置 -->
  75. <properties>
  76. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  77. </properties>
  78.  
  79. <dependencies>
  80. <dependency>
  81. <groupId>javax.servlet</groupId>
  82. <artifactId>servlet-api</artifactId>
  83. <version>2.5</version>
  84. <scope>provided</scope>
  85. </dependency>
  86.  
  87. <dependency>
  88. <groupId>org.apache.struts</groupId>
  89. <artifactId>struts2-core</artifactId>
  90. <version>2.5.1</version>
  91. </dependency>
  92.  
  93. </dependencies>
  94.  
  95. </project>

二,web.xml

  1. <!DOCTYPE web-app PUBLIC
  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4. <web-app>
  5. <display-name>Archetype Created Web Application</display-name>
  6. <init-param>
  7. <param-name>config</param-name>
  8. <param-value>../../resources/struts.xml</param-value>
  9. </init-param>
  10.  
  11. <filter>
  12. <filter-name>struts2</filter-name>
  13. <filter-class>
  14. org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
  15. </filter-class>
  16. </filter>
  17. <filter-mapping>
  18. <filter-name>struts2</filter-name>
  19. <url-pattern>/*</url-pattern>
  20. </filter-mapping>
  21.  
  22. <welcome-file-list>
  23. <welcome-file>login.jsp</welcome-file>
  24. </welcome-file-list>
  25. </web-app>

三,struts.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2.  
  3. <!DOCTYPE struts PUBLIC
  4. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  5. "http://struts.apache.org/dtds/struts-2.3.dtd">
  6.  
  7. <struts>
  8.  
  9. <constant name="struts.i18n.reload" value="false"/>
  10. <constant name="struts.devMode" value="false"/>
  11.  
  12. <include file="struts-default.xml"/>
  13.  
  14. <package name="default" extends="struts-default" namespace="/">
  15.  
  16. <action name="login" class="com.carson.demo.action.UserAction" method="login">
  17. <result name="success">index.jsp</result>
  18. <result name="login">login.jsp</result>
  19. </action>
  20.  
  21. </package>
  22.  
  23. </struts>

四,action

  1. package com.carson.demo.action;
  2.  
  3. import com.opensymphony.xwork2.ActionSupport;
  4. import org.apache.struts2.ServletActionContext;
  5.  
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.UnsupportedEncodingException;
  9.  
  10. public class UserAction extends ActionSupport {
  11.  
  12. private static final long serialVersionUID = 1L;
  13. private String msg;
  14.  
  15. public String getMessage() {
  16. System.out.println("测试有没有走");
  17. return msg;
  18. }
  19.  
  20. public String execute() {
  21. System.out.println("测试有没有走!!!!!!!!!!");
  22. msg = "哈哈,我是struts2";
  23. return SUCCESS;
  24. }
  25.  
  26. public String login() {
  27. try {
  28. HttpServletRequest request = ServletActionContext.getRequest();
  29. HttpServletResponse response = ServletActionContext.getResponse();
  30. request.setCharacterEncoding("UTF-8");
  31. response.setContentType("text/html;charset=utf-8");
  32. String username = request.getParameter("username");
  33. String password = request.getParameter("password");
  34. if ("admin".equals(username) && "123456".equals(password)) {
  35. return SUCCESS;
  36. } else {
  37. return "login";
  38. }
  39. } catch (UnsupportedEncodingException e) {
  40. e.printStackTrace();
  41. }
  42. return SUCCESS;
  43. }
  44. }

五,视图

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  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=UTF-8">
  7. <title>登录界面</title>
  8. </head>
  9.  
  10. <body>
  11. <form action="login" method="post">
  12. <table>
  13. <tr>
  14. <td>用户名:</td>
  15. <td><input type="text" name="username" /> </td>
  16. </tr>
  17. <tr>
  18. <td>密码:</td>
  19. <td><input type="text" name="password" /> </td>
  20. </tr>
  21. <tr>
  22. <td colspan="2">
  23. <input type="submit" value="登录" />
  24. <input type="reset" value="重置" /></td>
  25. </tr>
  26. </table>
  27. </form>
  28. </body>
  29. </html>
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8" isELIgnored="false" %>
  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=UTF-8">
  7. <title>Hello Maven</title>
  8. </head>
  9.  
  10. <body>
  11. ${message}
  12. </body>
  13. </html>

六,访问地址

http://localhost:8080/Strtus/

七,注意事项

在用${}取值的时候老是不成功

  1. 事情的真相是,你的页面没有开EL支持……
  2.  
  3. <%@ page language="java" import="java.util.*" pageEncoding="utf-8" isELIgnored="false"%>

maven+struts2环境搭建的更多相关文章

  1. 最新版ssh hibernate spring struts2环境搭建

    最新版ssh hibernate spring struts2环境搭建 最新版spring Framework下载地址:spring4.0.0RELEASE环境搭建 http://repo.sprin ...

  2. 第3章 Struts2框架--1、Struts2环境搭建

    第3章 Struts2框架--1.Struts2环境搭建 搭建步骤: 1.从下载http://struts.apache.org 没找到Struts2.3.16版,就下载了2.3.29 2.拷贝后解压 ...

  3. 【搬运工】之——Selenium+IDEA+Maven+TestNG环境搭建(转)

    Selenium+IDEA+Maven+TestNG环境搭建 第一 安装java环境. 1. 下载并安装Jdk1.7或Jdk1.8 http://www.oracle.com/technetwork/ ...

  4. TestNG+Maven+IDEA环境搭建

    TestNG+Maven+IDEA环境搭建 前言: 主要进行TestNG测试环境的搭建 所需环境: 1.IDEA UItimate 2.JDK 3.Maven 一.创建工程 File –>new ...

  5. 【SSH】 之 Struts2环境搭建及简单应用开发

    在上一篇文章中,我们一起了解了一下struts2的工作机制原理,接下来让我们进行一下简单应用的开发 (一)配置环境 1.建立web项目 2.导入jar包 其中struts2中有很多jar包,我们不需要 ...

  6. Spark Idea Maven 开发环境搭建

    一.安装jdk jdk版本最好是1.7以上,设置好环境变量,安装过程,略. 二.安装Maven 我选择的Maven版本是3.3.3,安装过程,略. 编辑Maven安装目录conf/settings.x ...

  7. struts2环境搭建和第一个程序

    环境搭建 项目目录 导入依赖jar包,如上图lib目录所示. 不同的版本可能会不一样,没关系在tomcat启动时,如果报错java.lang.ClassNotFoundException,我们可以按照 ...

  8. SSM Spring+SpringMVC+mybatis+maven+mysql环境搭建

    SSM Spring+SpringMVC+mybatis+maven环境搭建 1.首先右键点击项目区空白处,选择new->other..在弹出框中输入maven,选择Maven Project. ...

  9. selenium java maven testNg环境搭建

    maven获取jar的xml地址:http://mvnrepository.com 步骤一安装jdk(略) 步骤二 安装eclipse(略) 步骤三 安装testNG 步骤四 maven安装 步骤三 ...

随机推荐

  1. 525 Contiguous Array 连续数组

    给定一个二进制数组, 找到含有相同数量的 0 和 1 的最长连续子数组.示例 1:输入: [0,1]输出: 2说明: [0, 1] 是具有相同数量0和1的最长连续子数组. 示例 2:输入: [0,1, ...

  2. B. Code For 1 一个类似于线段树的东西

    http://codeforces.com/contest/768/problem/B 我的做法是,观察到,只有是x % 2的情况下,才有可能出现0 其他的,都是1来的,所以开始的ans应该是R - ...

  3. 【转】log4j的日志

    一.Log4j配置 第一步:加入log4j-1.2.8.jar到lib下. 第二步:在CLASSPATH下建立log4j.properties.内容如下: 放在src下的话就不用配置 否则得去web. ...

  4. Retrofit Upload multiple files and parameters

    Retrofit 的介绍以及基本使用 这里不再说明. 关于多文件上传 以及上传文件的同时携带多个参数说明 网上涉及到的不是太多. 上一张帅图: 代码: apiService: /** params 参 ...

  5. spoj GCJ1C09C Bribe the Prisoners

    题目链接: http://www.spoj.com/problems/GCJ1C09C/ 题意: In a kingdom there are prison cells (numbered 1 to  ...

  6. Oracle创建用户及权限设置

    oracle用户创建及权限设置 权限: create session create table unlimited tablespace connect resource dba 例: #sqlplu ...

  7. 浏览器报错问题解决:Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight respons

    FAQ: Failed to load http://www.erpshop.com/index.php: Request header field Content-Type is not allow ...

  8. MYSQL 二次筛选,统计,最大值,最小值,分组,靠拢

    HAVING 筛选后再 筛选 SELECT CLASS,SUM(TOTAL_SCORES) FROM student_score GROUP BY CLASS HAVING SUM(TOTAL_SCO ...

  9. macos openssl 生成rsa证书 -mark

    创建私钥 openssl genrsa -out rsa_private_key.pem 1024 创建无密码私钥 openssl pkcs8 -topk8 -inform PEM –nocrypt ...

  10. TUM好用的工具

    https://vision.in.tum.de/data/datasets/rgbd-dataset/tools?tdsourcetag=s_pctim_aiomsg