Eclipse 搭建Struts2

1.添加 struts  jar包

2.在WebContent文件夹下添加文加件page,在page文件夹下添加两个jsp,Come.jsp,Hello.jsp,用来区分执行的正确和错误

Come.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>Come on</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>
    Come on. <br>
  </body>
</html>
Hello.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>Hello word</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>
    Hello word. <br>
  </body>
</html>

3.创建一个Action,代码如下:

package com.ss02.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7752780747809187029L;

	
	@Override
	public String execute() throws Exception {
		System.out.println("execute");
		return super.execute();
	}
	
	public String login() throws Exception {
		System.out.println("登录");
		return SUCCESS;
	}
}


4.在添加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>
	<!-- struts的action配置文件 -->

	<!-- 所有的action都应该放在对应的package下 -->
	<package name="ss" extends="struts-default" namespace="/lzz">
		<action name="login" class="com.ss02.action.LoginAction">
			<!-- 定义逻辑视图和物理资源之间的映射 -->
			<result name="success">/page/Come.jsp</result>
			<result name="error">/page/Hello.jsp</result>
		</action>
		
		<action name="login_*" class="com.ss02.action.LoginAction" method="{1}">
			<!-- 定义逻辑视图和物理资源之间的映射 -->
			<result name="success">/page/Come.jsp</result>
			<result name="error">/page/Hello.jsp</result>
		</action>
	</package>
</struts>  

5.在web.xml中添加配置

      <!-- 定义struts2的核心filter -->  
    <filter>  
        <filter-name>struts2</filter-name>  
        <filter-class>  
            org.apache.struts2.dispatcher.FilterDispatcher              
        </filter-class>  
    </filter>  
    <!-- 让struts定义的核心filter拦截所有请求 -->  
    <filter-mapping>  
        <filter-name>struts2</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  

6.编写index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
	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 'index.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">
<title>Insert title here</title>
</head>

<body>
	This is my JSP page.
	<br>
	<form action="<%=basePath%>lzz/login">
		<input type="submit" value="login">
	</form>

	<form action="<%=path%>/lzz/login_login">
		<input type="submit" value="login_login">
	</form>
</body>
</html>

代码部分完成,启动tomcat。


关键点:

在struts.xml中添加了namespace=“lzz”,响应的在index.jsp页面访问的时候需要使用全局访问地址:

	<form action="<%=basePath%>lzz/login">
		<input type="submit" value="login">
	</form>

	<form action="<%=path%>/lzz/login_login">
		<input type="submit" value="login_login">
	</form>

<%=basePath%>lzz/login
<%=path%>/lzz/login_login


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值