美文网首页
Struts2配置

Struts2配置

作者: Yew | 来源:发表于2014-09-19 20:12 被阅读72次

上传jar包,复制jar包到WebRoot/WEB-INF/lib下

jar包 作用
commons-fileupload-1.2.2.jar 文件上传
commons-io-2.0.1.jar fileupload依赖于io
commons-logging-1.1.1.jar 关于日志处理的
ognl-3.0.5.jar ognl表达式
struts2-core-2.3.4.jar struts2框架核心库包
xwork-core-2.3.4.jar Xwork的核心类库
freemarker-2.3.19.jar 模板引擎
javassist-3.11.0.GA.jar 编辑Java字节码的类库
commons-lang3-3.1.jar 用来处理Java基本对象方法的工具类包

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>

在src下新建一个xml文件struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
    
<struts>
    <package name="struts2" extends="struts-default">
        <action name="login" class="com.sshlearn.action.LoginAction"> 
            <result name="success">/result.jsp</result>
        </action>
    </package>
</struts>

src下新建一个package com.sshlearn.action,并新建class loginAction

package com.sshlearn.action;

public class LoginAction {
    private String username;
    private String password;
    
    public String execute() {
        System.out.println(username+"==="+password);
        return "success";
    }
    
    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;
    }
}

新建login.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 'login.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>
      <form name="login" action="login" method="post">
        <input type="text" name="username"/>
        <br/>
        <input type="text" name="password"/>
        <br/>
        <input type="submit" value="保存" />
      </form>
  </body>
</html>

新建result.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 'result.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>
     username:${requestScope.username} <br/>
     password:${requestScope.password}
  </body>
</html>

项目目录结构如图

项目目录结构.jpg

访问地址localhost:8080/sshlearn/login.jsp,页面如下

login.jpg

跳转到result.jsp页面,结果如下

result.jpg

相关文章

网友评论

      本文标题:Struts2配置

      本文链接:https://www.haomeiwen.com/subject/evlutttx.html