gpt4 book ai didi

java - 使用 Spring MVC 在 JSP 中设置 HTML 链接

转载 作者:行者123 更新时间:2023-12-01 21:23:21 25 4
gpt4 key购买 nike

我尝试了 Spring MVC textbox example 上的教程。工作得很好,但是我不知道如何处理链接的名称。

唯一有效的是http://localhost:8080/SpringMVC/textbox.html,我不知道如何将textbox.html部分更改为其他任何东西。

根据评论和所有信息,我意识到它是根据控制类命名的。

TextBoxController.java -> textbox.html

相关代码如下:

web.xml

<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

mvc-dispatcher-servlet.xml

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

<bean class="nch.customer.controller.TextBoxController">
<property name="formView" value="CustomerForm" />
<property name="successView" value="CustomerSuccess" />
// Anything to put here?

<property name="validator">
<bean class="nch.customer.validator.CustomerValidator" />
</property>
</bean>

TextBoxController.java

public class TextBoxController extends SimpleFormController {

public TextBoxController() {
setCommandClass(Customer.class);
setCommandName("customerForm");
// Or anything to put here?
}

@Override
protected ModelAndView onSubmit (
HttpServletRequest request, HttpServletResponse response,
Object command, BindException errors) throws Exception {

Customer customer = (Customer) command;
return new ModelAndView("CustomerSuccess","customer",customer);
}

我的问题是。如何在不更改类名的情况下更改链接名称?例如:

TextBoxController.java -> txb.html

最佳答案

此 bean ControllerClassNameHandlerMapping 正在执行转换TextBoxController ->/textbox*

通过此配置,textbox.htmltextboxxx.html 都可以工作。

... to take the short name of the Class, remove the 'Controller' suffix if it exists and return the remaining text, lower-cased, as the mapping, with a leading /.

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

将其替换为另一个 bean SimpleUrlHandlerMapping,并在您想要自定义路径时为 TextBoxController bean 提供 id。您的 mvc-dispatcher-servlet.xml 将如下所示:

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/txb.html">txtboxController</prop>
</props>
</property>
</bean>

<bean id="txtboxController" class="nch.customer.controller.TextBoxController">
<property name="formView" value="CustomerForm" />
<property name="successView" value="CustomerSuccess" />
<property name="validator">
<bean class="nch.customer.validator.CustomerValidator" />
</property>
</bean>

关于java - 使用 Spring MVC 在 JSP 中设置 HTML 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38776920/

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