struts2框架搭建

一、需要导入的包:

commons-logging-1.0.4.jar

freemarker-2.3.8.jar

ognl-2.6.11.jar

struts2-core-2.0.11.jar

xwork-2.0.4.jar

二、配置文件

1。web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
    <display-name>Struts 2.0 Hello World</display-name>
    <filter>
        <filter-name>struts2</filter-name>      
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

2。struts.xml:

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <include file="struts-default.xml"/>
   
 <package extends="struts-default">
  <action >
   <result >/success.jsp</result>
   <result >/error.jsp</result>
   <result type="redirect">Login!error.action?key=${}</result>
  </action>  
 </package>
</struts>

注:<result type="redirect">Login!error.action?key=${}</result>

其中type="redirect"指的是可以按url重定向,其中当然可以写入带参数的URL了。

key必须在action中定义变量,${}是传参数的格式,可以写入变量的名字也可不写。

3。struts.properties

struts.devMode=true
struts.enable.DynamicMethodInvocation=true //允许使用动态方法
struts.custom.i18n.resources=label

4。Login.java

package com.pac.struts2;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class Login extends ActionSupport{
 private String message;
 private String userName;
 private String key;

 public String getMessage() {
  return message;
 }

 public void setMessage(String message) {
  this.message = message;
 }

 public String getUserName() {
  return userName;
 }

 public void setUserName(String userName) {
  this.userName = userName;
 }

 public String getKey() {
  return key;
 }

 public void setKey(String key) {
  this.key = key;
 }

 public String login(){     
        String message1 = " Hello, 请填写用户名!" ;
        HttpServletRequest request = ServletActionContext.getRequest();

        if(getUserName()==null || "".equals(getUserName())){
         return LOGIN;
        }
        message = getUserName()+"你好啊!!";
        return SUCCESS;
 }

 public String  error() throws Exception { 
   HttpServletRequest request = ServletActionContext.getRequest();
   String key = request.getParameter("key");
   if(key!=null && !"".equals(key)){
    setKey(key+"   你好啊!!");
   }else{
    setKey("mmm  你好啊!!");
   }
   message = getKey();
   return SUCCESS;
 }

}

5。页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix = "s"  uri = "/struts-tags" %>
<%
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>
    <font color="green">This is my success page.</font><br>
    <s:property value="message"/>
  </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值