我一直在努力工作这几天试图让它发挥作用.尝试将JSP与Spring Boot一起使用.我一直在github上关注这个例子.Web上有很多这样的例子都有一个web.xml文件,但github上的例子在这个论坛上被多次引用,甚至没有web.xml.此外,在创建Spring Boot项目时,STS不会创建具有子目录"WEB-INF"的"webapp"目录.我必须在Windows资源管理器中手动创建它们,然后在STS中刷新Project Explorer以使它们出现.
无论如何,我尽可能地遵循了这个例子,但是当我在地址字段中键入http:// localhost:8080时仍然无法在浏览器中呈现我的index.jsp文件.
任何指导将不胜感激.
这就是我到目前为止所拥有的.
目录结构:
application.properties:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/jeddatae_editor
spring.datasource.username=webapp
spring.datasource.password=secret
Run Code Online (Sandbox Code Playgroud)
的index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%
String name = request.getParameter("name");
%>
<!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=ISO-8859-1">
<title>JED Login</title>
</head>
<body>
<form action="/index" method="post">
<p>Insert ID here: <input type="text" id="id" name="id"></p>
<input type=submit>
</form>
<%
if(!name.equals(""))
{
%>
<div class="welcome">
<p>Welcome <%=name %> </p><p>If you are not <%=name %>, then try a different id.</p>
</div>
<%
}
%>
Run Code Online (Sandbox Code Playgroud)
为hello.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Hello</h2>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
GreetingController.java
package com.anypackage.jedcontext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;
import com.tacticalenterprisesltd.Database;
@Controller
public class GreetingController {
@RequestMapping("/")
public RedirectView index(@RequestParam(value="id", required=false) String id, RedirectAttributes attribs) {
if (id != null)
{
Database db = new Database("jeddatae_editor");
String[][] result = db.executeSelect("SELECT CONCAT(first_name, ' ', last_name) FROM employees WHERE id=" + id + ";");
if(result.length != 0){
attribs.addAttribute("name", result[0][0]);
}
}
return new RedirectView("index");
}
@RequestMapping("/hello")
public String hello() {
return "hello";
}
}
Run Code Online (Sandbox Code Playgroud)
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.anypackage</groupId>
<artifactId>jedtest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>jedtest</name>
<description>An application context test</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Tomcat embedded container-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- Need this to compile JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
我认为你首先需要处理重定向。因为在你的情况下new RedirectView("index")重定向到例如/index?name=Alan
添加方法:
@GetMapping("/index")
public String handleRedirect() {
return "index";
}
Run Code Online (Sandbox Code Playgroud)
如果您将其作为简单的 Spring Boot 应用程序运行,它应该可以工作:
mvn spring-boot:运行
但是如果您要在应用程序服务器上部署 war 文件,请不要忘记像这样配置:
@SpringBootApplication
public class JedtestApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(JedtestApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(JedtestApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1259 次 |
| 最近记录: |