maven+struts2项目简单演示

版权声明:本文为博主原创文章,借鉴的行人加关注 https://blog.csdn.net/GD_Tiny/article/details/77434499

一、首先是创建maven项目,就要搭建maven环境

1.把你下载好的maven版本解压,然后copy路径放入环境变量path:

         例如:本人路径是在:E:\install\apache-maven-3.5.0-bin\apache-maven-3.5.0-bin\apache-maven-3.5.0-bin\apache-maven-3.5.0\bin。

如图:

  2.然后是maven项目的在eclipse中引入

打开window 的perferences找到maven

3.配好后就建maven 项目,而今天我是演示一个简单的struts2登录的简单案例

创建maven项目,如图所示:(1)

(2)


3.


注意:创建之后项目会有错误,是因为一个servlet 的jar包没有引入依赖,name接下来我们要做的就是引入需要的依赖,接下来是代码演示:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>MyWeb_01</groupId>
  <artifactId>MyWeb_01</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>MyWeb_01 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    
    <!-- servlet -->
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.0-b07</version>
    <scope>provided</scope>
   </dependency>
   
   
   <!-- 注意这两个版本不宜过高,不然可能会报错 -->
   <!-- strtus依赖 -->
   <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.3.33</version>
   </dependency>
   
   <!-- mysql依赖 -->
   <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.6</version>
   </dependency>
   
    
    
  </dependencies>
  <build>
    <finalName>MyWeb_01</finalName>
  </build>
</project>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <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>*.action</url-pattern>
  </filter-mapping>
  
</web-app>

然后是struts.xml,位置位于javaRescourse:src/main/java

<?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.enable.DynamicMethodInvocation" value="true"></constant>
    	<package name="mypackage"  extends="struts-default" namespace="/">
    		<action name="login" class="com.tiny.action.UserAction">
    			<result name="success">index.jsp</result>
    		</action>
    	</package>
    </struts>


Loginaction

package com.tiny.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.tiny.entity.User;
import com.tiny.service.UserServiceI;
import com.tiny.service.UserServiceImp;

public class UserAction  extends ActionSupport{
	
 public String login() throws Exception {
		
		UserServiceI userServive=new UserServiceImp();
		//解决乱码
		HttpServletRequest req=ServletActionContext.getRequest();
		req.setCharacterEncoding("UTF-8");
		
		HttpServletResponse resp=ServletActionContext.getResponse();
		resp.setContentType("text/html; charset=UTF-8");
		//获取值
		String u_name=req.getParameter("u_name");
		String u_pass=req.getParameter("u_pass");
		boolean b=userServive.userLogin(new User(u_name, u_pass));
		if(b){
			System.out.println("登录成功!");
		}else{
			System.out.println("登陆失败!");
		}
		return "success";
	}
	
}

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>登录</title>
</head>
<body>
	<div align="center">
		<form action="login!login.action" method="post">
			账号:<input type="text" name="u_name"><br>
			密码:<input type="password" name="u_pass"><br>
			<input type="submit" value="登录">
		</form>
	</div>
</body>
</html>

好了,到这就告一段落了,有问题追加哦!



没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试