IDEA创建spring加struts2项目
选择spring和struts,注意版本,不同的struts版本,过滤器的位置不一样
选择存放位置,并点击完成创建项目,在创建过程中会自动下载相关jar
初始化完成后的目录结构为
修复生成的web.xml文件
1.检查生成的struts2过滤器和选择的版本是否一致(现在我的是不匹配的需要修改StrutsPrepareAndExecuteFilter的位置)
2.spring的监听器ContextLoaderListener,因为没有生成spring-web-4.3.18.RELEASE.jar,所有要下载spring-web-4.3.18.RELEASE.jar,spring框架下载地址https://repo.spring.io/release/org/springframework/spring/
新的web.xml文件为:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd"> <display-name>struts2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
现在生成的路径还没还没有生成lib里面的jar包,配置IDEA生成路径
在src目录里新建一个处理请求的action,取名叫LoginAction.java
import com.opensymphony.xwork2.ActionSupport; /**
* @author mx
* @description: TODO
* @date 2019-06-26 10:43
*/
public class LoginAction extends ActionSupport { private String username; private String password; 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;
} @Override
public String execute() throws Exception {
if (username.equals("admin") && password.equals("123456")) {
return SUCCESS;
} else {
return LOGIN;
} }
}
增加sping容器默认的配置文件(也可以指定文件名,这里就直接使用默认的文件名)applicationContext.xml,在里面注入创建的action
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="userClass" class="LoginAction" scope="prototype"/> </beans>
在struts.xml配置struts2的映射关系
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="Login" class="LoginAction">
<result name="success">/WEB-INF/jsp/success.jsp</result>
<result name="login">/index.jsp</result>
</action>
</package>
</struts>
在WEB-INF目录下面新建jsp目录,然后在创建success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>${username}登录成功</title>
</head>
<body>
<h1>登录成功!</h1>
</body>
</html>
修改index.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户登录</title>
</head>
<body>
<h1>用户登录</h1>
<form action="Login.action" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2" style="text-align: center"><input type="submit" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>
本来以为在这点就配置完成了,然后配置tomcat运行项目:
结果出现错误:
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
ERROR Dispatcher Dispatcher initialization failed
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.inject(ContainerImpl.java:289)
at com.opensymphony.xwork2.inject.ContainerImpl$ConstructorInjector.construct(ContainerImpl.java:422)
at com.opensymphony.xwork2.inject.ContainerBuilder$5.create(ContainerBuilder.java:231)
at com.opensymphony.xwork2.inject.Scope$2$1.create(Scope.java:52)
at com.opensymphony.xwork2.inject.ContainerBuilder$3.create(ContainerBuilder.java:106)
at com.opensymphony.xwork2.inject.ContainerBuilder$7.call(ContainerBuilder.java:584)
at com.opensymphony.xwork2.inject.ContainerBuilder$7.call(ContainerBuilder.java:581)
at com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:560)
at com.opensymphony.xwork2.inject.ContainerBuilder.create(ContainerBuilder.java:581)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.createBootstrapContainer(DefaultConfiguration.java:287)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:162)
然后查阅资料,有说,添加classpath下的目录添加一个log4j2.xml,有说映入log4j-core包的,但是最后都没有解决,最新想是不是自动stuts2包的问题,就去官网https://struts.apache.org/download.cgi下载最新的试试,下载文件
先删除原来structs2 的jar
解压下载的文件,引入新的jar
然后在运行tomcat得到正确的页面
输入正确的用户名和密码后,跳转
IDEA创建spring加struts2项目的更多相关文章
- 创建Spring Cloud聚合项目
使用maven创建单一项目的时候通常用不到聚合项目,创建spring cloud项目时候,由于下面都是一个一个微服务,每个服务对应一个项目,这就需要用到聚合项目,方便对依赖和项目之间的关系进行管理,使 ...
- IDEA创建Spring Boot的项目
IDEA创建SpringBoot的项目非常的方便智能,可以实现零配置,只需要在创建的时候勾选你需要的功能,比如mybatis,mysql等等,它会帮你自动下载导入响应的jar,不用自己再去手动填写. ...
- (Struts2学习系列一)MyEclipse创建第一个struts2项目
点击MyEclipse菜单栏File按钮,点击new-->Web Project 输入Project name之后点击Finish 项目创建完成. 然后右键项目,点击MyEclipse--> ...
- Spring 加载项目外部配置文件
背景 在项目的部署过程中,一般是打成 war 或者 jar 包,这样一般存在两种问题: 即使是配置文件修改,也还需要整个项目重新打包和部署. 整个项目只有一套环境,不能切换. 针对上面的问题,可以使用 ...
- eclipse环境下基于已构建struts2项目整合spring+hibernate
本文是基于已构建的struts2项目基础上整合 spring+hibernate,若读者还不熟悉struts2项目,请先阅读 eclipse环境下基于tomcat-7.0.82构建struts2项目 ...
- Spring入门案例 idea创建Spring项目
spring入门案例 idea创建spring项目 Spring介绍 Spring概述 Spring是一个开源框架,Spring是2003年兴起的轻量级java开发框架,由Rod Johnson 在其 ...
- Spring Boot入门(一):使用IDEA创建Spring Boot项目并使用yaml配置文件
由于公司最近在做技术转型(从.Net转Java),因此自己也开启了学习Java之路.学习Java怎么能不学习这几年这么火的Spring Boot框架,由于自己有总结的习惯,因此会把学习的过程以博客的形 ...
- Spring入门(一):创建Spring项目
本篇博客作为Spring入门系列的第一篇博客,不会讲解什么是Spring以及Spring的发展史这些太理论的东西,主要讲解下如何使用IntelliJ IDEA创建第一个Spring项目以及通过一个示例 ...
- Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)。在我的上一篇博文中介绍的通过web工厂的方式获取servcie的方法因为太麻烦,所以开发的时候不会使用。
1. spring整合struts的基本操作见我的上一篇博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2 ...
随机推荐
- Analysis Methods in Neural Language Processing: A Survey
本文对神经语言处理中的分析方法进行了综述,并根据研究的突出趋势对其进行了分类,指出了存在的局限性,指出了今后研究的方向.
- spring5源码分析系列(二)——spring核心容器体系结构
首先我们来认识下IOC和DI: IOC(Inversion of Control)控制反转:控制反转,就是把原先代码里面需要实现的对象创建.依赖的代码,反转给容器来帮忙实现.所以需要创建一个容器,并且 ...
- Linux学习笔记(15)Linux字符集(locale,LANG,LC_ALL)
关键词:linux系统修改编码,linux字符集问题, 目录 零.什么是locale 一.locale的详细内容 二.理解locale的设置 三 具体设定locale的方法(zh_CN.UTF-8. ...
- NIKKEI Programming Contest 2019-2 Task E. Non-triangular Triplets
$\require{enclose}$ 必要条件 一方面 $\sum_{i=1}^{N}(a_i + b_i) \le \sum_{i=1}^{N} c_i \implies 2\sum_{i=1}^ ...
- 【一道来自老师的题的题解】equip——奇妙的最短路
这道题真的第一眼完全想不到是最短路啊!!!!!!!! 感谢DR大佬讲解!!!!!90°鞠躬 =u= 暂时没有评测网址,(因为需要special judge)敬请期待 机房另一大佬JYY题解,可以对比参 ...
- AppCan模拟器调试
来源: 1, 页面CSS调试 2, JS调试 3, 插件请打包后手机调试 4, 打开另一个页面示例: appcan.button("#myBtn", "ani-uct&q ...
- java检测是不是移动端访问
request可以用别的代替 private static boolean isMobile(){ HttpServletRequest request = ThreadContextHolder.g ...
- CSS3点击波浪按钮特效
在线演示 本地下载
- Codeforces 1194C. From S To T
传送门 首先贪心, $S$ 能和 $T$ 匹配就要尽量匹配,剩下的才让 $P$ 来补 在 $S$ 全部匹配上的情况下,看看 $P$ 是否有足够的字符即可 #include<iostream> ...
- Chromium浏览器启动参数
序号 参数 说明1 --allow-outdated-plugins 不停用过期的插件.2 --allow-running-insecure-content 默认情况下,https 页面不允许从 ht ...