Struts2简单配置

这里介绍Eclipse 的 Struts2的配置
工程源码会在博文最后给出。

1、下载相应的jar包,并将jar包增加到构建路径
下载地址:点击下载

2、编辑web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>Struts2</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>

	<!-- Struts2的配置 -->
	<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>*.jsp</url-pattern>
	</filter-mapping>

	<filter-mapping>
		<filter-name>Struts2</filter-name>
		<url-pattern>*.js</url-pattern>
	</filter-mapping>

	<filter-mapping>
		<filter-name>Struts2</filter-name>
		<url-pattern>*.action</url-pattern>
	</filter-mapping>

	<filter-mapping>
		<filter-name>Struts2</filter-name>
		<url-pattern>*.action</url-pattern>
	</filter-mapping>


</web-app>

3、在src文件夹下新建struts.xml【这里命名一定要正确!而且一定要在src文件夹下!】
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>
	
	<constant name="struts.devMode" value="true"/>
	<!-- struts2的action必须放在一个指定的包空间下定义 -->
	<package name="Struts2 Login" extends="struts-default">
		<!-- 定义处理请求URL为login.action的Action -->
		<action name="login" class="controller.Login">
			<!-- 定义处理结果字符串和资源之间的映射关系 -->
			<result name="ok.jsp">/ok.jsp</result>
			<result name="no.jsp">/no.jsp</result>
		</action>
	</package>
</struts>

4、创建controller包,并在下面创建Login.java类

package controller;

import service.UserInfoService;

public class Login {
	private String username;
	private String password;

	// 方法名一定为execute,返回类型一定是String
	public String execute() {
		UserInfoService userInfoService = new UserInfoService();
		if (userInfoService.login(username, password))
			return "ok.jsp";
		return "no.jsp";
	}

	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;
	}
}

5、创建service包,并创建UserInfoService类
UserInfoService.java:

package service;

public class UserInfoService {
	public boolean login(String username, String password) {
		if (username.equals(password))
			return true;
		return false;
	}
}

6、创建login.jsp、ok.jsp、no.jsp
login.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="login.action" method="post">
		username: <input type="text" name="username"> <br /> password:
		<input type="password" name="password"> <br /> 
		<input type="submit" value="登录">
	</form>
</body>
</html>

ok.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	ok page!
</body>
</html>

no.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%response.sendRedirect("ok.jsp"); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	no.page!
</body>
</html>

工程源码下载地址:点击下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值