gpt4 book ai didi

java - Netbeans Spring 4.0 需要 GET 和 POST

转载 作者:行者123 更新时间:2023-12-02 13:22:48 24 4
gpt4 key购买 nike

我意识到这是基本的。我完全一无所知,找不到任何更新的简单教程。

使用 Spring 4.0 和 Netbean 的 Spring 默认内容...

  1. 如何让controller.DefaultController处理各种URI的GETPOST请求?

  2. 如何让${msg}出现?

applicationContext.xml

    <?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">




<context:annotation-config/>
<context:component-scan base-package="learn"/>

<bean id="test" class="learn.Test"></bean>

<!--bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" /-->

<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->

</beans>

调度程序-servlet.html

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
<prop key="index.htm">indexController</prop>
<prop key="index">indexController</prop>
<prop key="index.html">indexController</prop>
<prop key="submitPage.html">indexController</prop>
</props>
</property>
</bean>

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />

<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />

<!--
Changing to this does not work. Why? What should I do?
<bean name="indexController"
class="controller.DefaultController"/>
-->

</beans>

WEB-INF/jsp/index.jsp

<%@page contentType="text/html" 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>Welcome to Spring Web MVC project</title>
</head>

<body>
<p>Hello! This is the default welcome page for a Spring Web MVC project.</p>
<p><i>To display a different welcome page for this project, modify</i>
<tt>index.jsp</tt> <i>, or create your own welcome page then change
the redirection in</i> <tt>redirect.jsp</tt> <i>to point to the new
welcome page and also update the welcome-file setting in</i>
<tt>web.xml</tt>.</p>

<p>msg: ${msg}</p>

<form action="/submitPage.html">
<input type="text" id="zzz" name="zzz">
<input type="submit" value="Submit!!">
</form>
</body>
</html>

src/controller/DefaultController

package controller;

import learn.Test;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DefaultController {

@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(ModelMap map) {

map.put("msg", "/");
return "grrr";
}


@RequestMapping(value = "/index", method = RequestMethod.GET)
public String indexPure(ModelMap map) {

map.put("msg", "/index");
return "qwerty";
}


@RequestMapping(value = "/index.html", method = RequestMethod.GET)
public String indexHTML(ModelMap map) {

map.put("msg", "/index.html");
return "asdfgh";
}


@RequestMapping(value="/submitPage.html", method=RequestMethod.POST)
public @ResponseBody Object box(@RequestBody Test testRequest) {
//Do stuff here
return testRequest.getResult();
}
}

src/学习/测试

package learn;
import org.springframework.stereotype.Component;
@Component
public class Test {
private static final long serialVersionUID = 1000000;

private String zzz;
public String getZzz(){return zzz;}
public void setZzz(String zzz){this.zzz = zzz;}

public String getResult(){
return "result = " + zzz;
}
}

index.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<%@ page language="java" session="false" contentType="text/html;charset=utf-8" %>
<jsp:forward page='index.html'/>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>TODO supply a title</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
<div>TODO write content</div>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
</web-app>

文件: LearnSpring

最佳答案

您正在混合 Handler Mappings 的 XML 配置和基于注释的配置,这是可能的,但必须小心行事,目前它们重叠并且很难说发生了什么。我想您从使用 XML 的示例开始,然后继续使用一些基于注释的代码。我还不明白 submitPage 是什么意思? ,我将重构一个只有一个映射的基本示例 get-post /index.html ,用注释声明。

applicationContext.xml可以留空

dispatcherServlet.xml必须仅包含两个 bean:

       <bean name="indexController" class="controller.DefaultController"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />

ParameterizableViewController ,仅用于非常简单的页面,是不需要的。还有 SimpleUrlHandlerMapping 的映射不需要:处理程序映射现在仅由 Controller 中的注释定义。

DefaultController可能会变成:

@Controller
public class DefaultController {

@RequestMapping(value = "/index.html", method = RequestMethod.POST)
public String indexPure(ModelMap map, HttpServletRequest request) {

final String zzz = request.getParameter("zzz");
map.put("msg", "You sent [" + zzz + "]");
return "index";
}


@RequestMapping(value = "/index.html", method = RequestMethod.GET)
public String indexHTML(ModelMap map) {

map.put("msg", "Please send something");
return "index";
}

}

在这些 Controller 方法中,返回值是 View 名称,而模型用于传递 msg到 View 。看看@RequestMapping Javadoc了解支持的其他参数的列表。

在JSP页面中:

  • method='post'失踪了。
  • 您不能使用/submitPage.html/index.html因为 URL 必须相对于 context-path 进行解析,所以使用

    <form method='post' action="<c:url value='/index.html'/>">
  • 使用<c:out value='${msg}'/>对消息进行 html 转义

使用<c:标签在顶部声明 <%@page 之后指令:

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

然后是index.xhtml页面应替换为 index.jsp包含:

        <%@ page
language="java"
session="false"
contentType="text/html;charset=utf-8" %>
<jsp:forward page='index.html'/>

根据 web.xml 中声明的 servlet 版本,您在运行 JSP 时可能会遇到问题。如果是这种情况,请提供有关 web.xml 和 pom.xml 的更多信息

在 web.xml 中模式是错误的

   <url-pattern>*.htm</url-pattern>

使用

  <url-pattern>/</url-pattern>

不确定是否使用redirect.jsp作为欢迎页面,使用

    <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

并使用index.jsp转发或添加index.xhtml到列表

关于java - Netbeans Spring 4.0 需要 GET 和 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43516754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com