首页 > 代码库 > maven+struts2例子

maven+struts2例子

本文在开发第一个maven示例的基础上进行扩展。

第一个maven示例目录结构如下:

技术分享

扩展后目录为:

技术分享

 


打开pom.xml

技术分享

技术分享

在WEB-INF文件夹下新建web.xml:

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0"     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_3_0.xsd">  <display-name></display-name>      <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\main\resources文件夹下新建struts.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts>    <constant name="struts.devMode" value="true" />    <package name="base" extends="struts-default">        <action name="login" class="base.web.action.login.LoginAction"            method="login">            <result name="success">WEB-INF/success.jsp</result>            <result name="error">WEB-INF/error.jsp</result>        </action>    </package></struts>    

在src\main\java文件夹下新建LoginAction类

package base.web.action.login;public class LoginAction {    private String username;    private String password;    public String login() {        if (username.equalsIgnoreCase("111")                && password.equalsIgnoreCase("222")) {            return "success";        }        return "error";    }    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;    }}

修改index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"   pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%>   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>登录</title></head><body>   <h1>登 录</h1>   <p>帐号: 111,  密码:222 </p>   <form action="login">    帐号: <input type="text" name="username"/><br/>      密码: <input type="password" name="password"/><br/>        <input type="submit" value="提交"/>   </form></body></html>

在WEB-INF文件夹下添加success.jsp和error.jsp

success.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><html><head><title>登录</title></head><body>    登录成功!!!</body></html>

error.jsp:

<%@ page contentType="text/html; charset=UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><html><head><title>登录</title></head><body>    登录失败!!!</body></html>

将项目部署至tomcat,然后浏览器访问:http://localhost:8080/mavenDemo,出现以下页面:

技术分享

当输入账号:111,密码:111时,出现以下页面:

技术分享

当输入账号:111,密码:222时,出现以下页面:

技术分享

 

maven+struts2例子